我有我的用戶定義類的列表。比較在Arraylist
class Customer{
Integer age;
String name;
//getter & setter
}
Collections.sort(customerList, new Comparator <Customer>() {
public int compare(Customer o1, Customer o2) {
// TODO Auto-generated method stub
if(o1.getAge()!=null && o2.getAge() != null)
return o1.getDistance().compareTo(o2.getDistance());
else
return 1;
}
});
現在我的年齡可變可能有空值或年齡客戶的。所有null值應該是追加在最後,其餘的值應該排序在升序或降序(任何事都可以)?
但是這個代碼是拋出異常:
java.lang.IllegalArgumentException: Comparison method violates its general contract!
請告訴我該怎麼辦? 在此先感謝。
全堆棧跟蹤請... – Andremoniy
看看http://stackoverflow.com/questions/8327514/comparison-method-violates-其一般合同 – cubbuk