我有我的用户定义类的列表。比较在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