Collections.sort(lcd1, new Comparator<CouponDto>(){
@Override
public int compare(CouponDto o1, CouponDto o2) {
if(o1.getCoupon().getListOrder2() > o2.getCoupon().getListOrder2()){
return -1;
}
if(o1.getCoupon().getListOrder2() == o2.getCoupon().getListOrder2()){
return 0;
}
return 1;
};
});
報Comparison method violates its general contract異常
改成
Collections.sort(lcd1, new Comparator<CouponDto>(){
@Override
public int compare(CouponDto o1, CouponDto o2) {
return -(o1.getCoupon().getListOrder2() - o2.getCoupon().getListOrder2());
};
});
就不再報錯;
各位大神,這個是怎么回事
1 回答

不負相思意
TA貢獻1777條經驗 獲得超10個贊
你的代碼雖然滿足了自反性,但是不滿足偏序。jdk 1.7還要求偏序性。 比如a = 10 b = 8 c = 4,那么你的代碼出現 comp(a,b) == comp(a, c),sort會假設b=c但是實際上是b>c
添加回答
舉報
0/150
提交
取消