2014-03-27 118 views
0

我一直在尋找several tutorials,他們似乎都有不同的編寫比較函數的算法。下面是一些例子:如何爲比較器編寫比較函數

public int compareTo(Fruit compareFruit) { 

    int compareQuantity = ((Fruit) compareFruit).getQuantity(); 

    //ascending order 
    return this.quantity - compareQuantity; 

    //descending order 
    //return compareQuantity - this.quantity; 

} 

and

@Override 
    public int compare(Employee e1, Employee e2) { 
     return (int) (e1.getSalary() - e2.getSalary()); 
    } 

是有一個 「嚮導」,它可以自動生成呢?

+1

沒有嚮導可以弄清楚_you_想怎麼比較東西。 –

+2

對象的平等取決於您的應用程序要求。 – kosa

回答