[英]Comparable interface with many conditions
The Question is how can use comparable interface and collections.sort to do the sorting with model , production and price. 问题是如何使用可比较的接口和collections.sort对模型,产量和价格进行分类。 Can i do these three sorting in ascending order within "public int compareto(car other)"? 我可以在“ public int compareto(car other)”中按升序进行这三种排序吗?
For example, It will be sorted with model in alphabetical order. 例如,它将按字母顺序与模型一起排序。 If model is same, then sorted with production in alphabetical order. 如果型号相同,则按字母顺序与生产进行排序。 if production is also same , then finally sorted with price in ascending order. 如果产量也相同,则最终按价格升序排序。
Thank you for attention, i stuck with this problem many days. 谢谢您的关注,我困扰了很多天。 Please help me. 请帮我。
public static void main(String[] args) {
ArrayList<Car> car = new ArrayList<car>();
// something ignored//
Collections.sort(car); <----------------------Problem
for (Car c : car) {
System.out.println(c);
}
}
class car implements Comparable<car>{
protected String model;
protected String production;
protected int price;
public Tablet(String model ,String production , int price)
{
this.model=model;
this.price=price;
this.production = production;
}
public int compareTo (car other)
{
?????????????????
}
}
class mini-bus extends car
{
private door;
public Tablet(String model ,String production , int price ,int door)
{
super(model , production , price);
this.door = door;
}
}
The principle is quite straightforward: 原理很简单:
compare
value; 如果它们不同,则返回负/正compare
值。 otherwise... 除此以外... compare
value; 如果它们不同,则返回负/正compare
值。 otherwise... 除此以外... compare
value. 这是最后一个属性,因此返回compare
值。 For example: 例如:
int compareModels = this.model.compareTo(that.model);
if (compareModels != 0) {
return compareModels;
}
int compareProd = this.production.compareTo(that.production);
if (compareProd != 0) {
return compareProd;
}
return Integer.compare(this.price, that.price);
Note that there is also a nice class in Guava called ComparisonChain
which reduces a lot of this boilerplate logic: 请注意,Guava中还有一个不错的类,名为ComparisonChain
,它减少了许多样板逻辑:
return ComparisonChain.start()
.compare(this.model, that.model)
.compare(this.production, that.production)
.compare(this.price, that.price)
.result();
This stops comparing once a difference is found between any pair of properties. 一旦发现任何一对属性之间的差异,这将停止比较。 It will still access the subsequent properties, but that should hopefully be an irrelevantly cheap thing to do anyway. 它仍然会访问后续的属性,但是无论如何这应该是一件无关紧要的廉价事情。
Here is the general approach to the problem of multi-attribute sorting: 这是解决多属性排序问题的一般方法:
If the number of attributes is fixed, the "loop" on the ordered list of attributes is unrolled, ie each individual attribute is compared separately: 如果属性的数量是固定的,则将展开属性的有序列表上的“循环”,即分别比较每个单独的属性:
int res;
res = this.getA().compareTo(other.getA()); // e.g. model
if (res != 0) return res;
res = this.getB().compareTo(other.getB()); // e.g. production
if (res != 0) return res;
res = this.getC().compareTo(other.getC());
if (res != 0) return res;
...
// For the last attribute return the result directly
return this.getZ().compareTo(other.getZ()); // e.g. price
This should do: 应该这样做:
public int compareTo(Car other){
if(this.getModel().compareTo(other.getModel()) != 0){
return this.getModel().compareTo(other.getModel());
}else if(this.getProduction().compareTo(other.getProduction()) != 0){
return this.getProduction().compareTo(other.getProduction());
}else{
return Integer.compare(this.getPrice(), other.getPrice());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.