java comparator 排序 定義比較器內部類的方式排序 通用排序方式 非常靈活
class Products implements Comparable<Products> {
private String name;
private int num;
private Integer price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public Products(String name, int num, Integer price) {
super();
this.name = name;
this.num = num;
this.price = price;
}
@Override
public String toString() {
return "Products [name=" + name + ", num=" + num + ", price=" + price+ "]\n";
}
public Products() {
super();
}
@Override
public int compareTo(Products o) {
// 按價格升序排列
//如果當前物件的價格小於引數物件的價格 返回-1
//如果當前物件的價格等於。。。。。。返回0
//如果當前物件的價格大於。。。。。。返回1
return this.price<o.price?-1:this.price>o.price?1:0;
}
----------------------測試程式碼如下 :
/**
* 測試動態改變比較邏輯
*/
@Test
public void testComparator(){
List<Products> products=new ArrayList<Products>();
products.add(new Products("費羅列",100,30));
products.add(new Products("love",1000,100));
products.add(new Products("德芙",10000000,10));
products.add(new Products("自行車",10000,899));
products.add(new Products("行李箱",8000,569));
products.add(new Products("泳衣",100,180));
//定義比較器 按價格降序
Comparator<Products> byPrice=new Comparator<Products>() {
@Override
public int compare(Products o1, Products o2) {
return o1.getPrice()<o1.getPrice()?-1:o1.getPrice()>o2.getPrice()?1:0;
}
};
//定義比較器 按銷量降序
Comparator<Products> byNum=new Comparator<Products>() {
@Override
public int compare(Products o1, Products o2) {
return o1.getNum()<o2.getNum()?-1:o1.getNum()>o2.getNum()?1:0;
}
};
//定義比較器 按銷量降序 再按價格升序排列 當銷量相等時不返回0 再繼續比較單價
Comparator<Products> byNumDes_price=new Comparator<Products>() {
@Override
public int compare(Products o1, Products o2) {
if(o1.getNum()!=o2.getNum()){
return -(o1.getNum()<o2.getNum()?-1:1);
}else{
return o1.getPrice()>o2.getPrice()?1:o1.getPrice()<o2.getPrice()?-1:0;
}
}
};
Collections.sort(products,byPrice);
System.out.println(products);
Collections.sort(products,byNum);
System.out.println(products);
Collections.sort(products,byNumDes_price);
System.out.println(products);
}
-------------------輸出內容如下:
[Products [name=德芙, num=10000000, price=10]
, Products [name=費羅列, num=100, price=30]
, Products [name=love, num=1000, price=100]
, Products [name=泳衣, num=100, price=180]
, Products [name=行李箱, num=8000, price=569]
, Products [name=自行車, num=10000, price=899]
]
[Products [name=費羅列, num=100, price=30]
, Products [name=泳衣, num=100, price=180]
, Products [name=love, num=1000, price=100]
, Products [name=行李箱, num=8000, price=569]
, Products [name=自行車, num=10000, price=899]
, Products [name=德芙, num=10000000, price=10]
]
[Products [name=德芙, num=10000000, price=10]
, Products [name=自行車, num=10000, price=899]
, Products [name=行李箱, num=8000, price=569]
, Products [name=love, num=1000, price=100]
, Products [name=費羅列, num=100, price=30]
, Products [name=泳衣, num=100, price=180]
]