为什么我的比较方法会引发异常 -- 比较方法违反了其总协定!
为什么此代码
public class SponsoredComparator implements Comparator<SRE> {
public boolean equals(SRE arg0, SRE arg1){
return arg0.getSponsored()==arg1.getSponsored();
}
public int compare(SRE object1, SRE object2) {
Log.d("SponsoredComparator","object1.getName() == "+ object1.getName());
Log.d("SponsoredComparator","object1.getSponsored() == "+ object1.getSponsored());
Log.d("SponsoredComparator","object2.getName() == "+ object2.getName());
Log.d("SponsoredComparator","object2.getSponsored() == "+ object2.getSponsored());
Log.d("SponsoredComparator","compare return == "+ (object1.getSponsored() && object2.getSponsored() ? 0 : object1.getSponsored() ? -1 : 1));
return object1.getSponsored() && object2.getSponsored() ? 0 : object1.getSponsored() ? -1 : 1;
}
}
引发此异常:ERROR/AndroidRuntime(244): java.lang.IllegalArgumentException: Comparison method violates its general contract!
ERROR/AndroidRuntime(4446): at java.util.TimSort.mergeLo(TimSort.java:743)
方法 sre.getSponsored() 返回一个布尔值。
谢谢。