简体   繁体   English

具有许多条件的可比接口

[英]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 the first pair of properties. 比较第一对属性。 If they are different, return the negative/positive compare value; 如果它们不同,则返回负/正compare值。 otherwise... 除此以外...
  • Compare the second pair of properties. 比较第二对属性。 If they are different, return the negative/positive compare value; 如果它们不同,则返回负/正compare值。 otherwise... 除此以外...
  • ... (repeat for as many pairs of properties as you have) ... ...(重复使用尽可能多的属性对)...
  • Compare the last pair of properties. 比较最后一对属性。 This is the last property, so return the 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: 这是解决多属性排序问题的一般方法:

  • Decide on the ordered list of attributes by which you sort 确定排序的属性的有序列表
  • For each attribute on your list, compare the values on both sides 对于列表中的每个属性,比较双方的值
  • If the result is not zero, return it right away 如果结果不为零,请立即返回
  • If the result is zero, go to the next attribute on your list 如果结果为零,请转到列表中的下一个属性
  • If you ran out of attributes, return zero 如果没有足够的属性,则返回零

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM