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;

}

  */

 
@Override //重写降序方式
public int compareTo(Products o) {
// 按价格升序排列
//如果当前对象的价格小于参数对象的价格 返回-1
//如果当前对象的价格等于。。。。。。返回0
//如果当前对象的价格大于。。。。。。返回1
return this.price<o.price?1:this.price>o.price?-1:0;
}

}

本文转载:CSDN博客