我的排序方法出現錯誤。比較方法違反了其在排序方法中的一般合同
比較法違反其總承包
這是我用的排序方法排序對象
public abstract class ComparablePerson extends IDValueItem implements
Comparable<ComparablePerson> {
private int score;
private String itemID,itemName;
//setters and getters
public int compareTo(ComparablePerson another) {
if (score == another.getScore())
return this.getItemName().compareToIgnoreCase(another.getItemName());
else if ((score) > another.getScore())
return 1;
else
return -1;
}
@Override
public boolean equals(Object o) {
final ComparablePerson other = (ComparablePerson) o;
if (score == other.getScore() && this.getItemName().equalsIgnoreCase(other.getItemName()))
return true;
else
return false;
}
我只是叫 Collections.sort(ComparablePersonCollection);
這可能是什麼原因?
這固定它,THX) –