【发布时间】:2012-12-07 12:59:50
【问题描述】:
我的排序方法出错。
比较方法违反了它的一般约定
这是我使用排序方法的排序对象
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);
这可能是什么原因?
【问题讨论】: