package sort.refType;
/**
- 无接口的实体类
- @author Administrator
*/
public class Goods {
//商品名称
private String name;
//价格
private double price;
//收藏量
private int fav;
public Goods() {
super();
}
public Goods(String name, double price, int fav) {
super();
this.name = name;
this.price = price;
this.fav = fav;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getFav() {
return fav;
}
public void setFav(int fav) {
this.fav = fav;
}
@Override
public String toString() {
return "商标名:" + name+ ", 收藏量:" + this.fav + ", 价格:" +this.price+"\n";
}
}
//------------------------------------------------------
package sort.refType;
/**
-
按价格排序的业务类(降序)
-
@author Administrator
*GoodsPriceComp 商品价格比较
*/
public class GoodsPriceComp implements java.util.Comparator{@Override
public int compare(Goods o1, Goods o2) {//重写return -((o1.getPrice()-o2.getPrice())>0?1:(o1.getPrice()==o2.getPrice()?0:-1));//吗默认是升序;降序加负号
}
}
//----------------------------------------------
package sort.refType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class GoodsApp {
public static void main(String[] args) {
List list = new ArrayList();
list.add(new Goods("李四视频",2000,0));
list.add(new Goods("王伟视频",4000,50));
list.add(new Goods("李静视频",3000,60));
System.out.println("排序前:"+list);
//价格排序
Collections.sort(list,new GoodsPriceComp());//GoodsPriceComp商品价格比较
System.out.println("排序后:"+list);
//收藏量排序
//Collections.sort(list,new GoodsFayComp());
//System.out.println("排序后:"+list);
}
}
//结果-------------------------------------------------------
排序前:[商标名:李四视频, 收藏量:0, 价格:2000.0
, 商标名:王伟视频, 收藏量:50, 价格:4000.0
, 商标名:李静视频, 收藏量:60, 价格:3000.0
]
排序后:[商标名:王伟视频, 收藏量:50, 价格:4000.0
, 商标名:李静视频, 收藏量:60, 价格:3000.0
, 商标名:李四视频, 收藏量:0, 价格:2000.0
]