在JDK7版本以上,Comparator要知足自反性,传递性,对称性,否则Arrays.sort,Collections.sort会报IllegalArgumentException异常。ide
说明:code
1) 自反性:x,y的比较结果和y,x的比较结果相反。get
2) 传递性:x>y,y>z,则x>z。io
3) 对称性:x=y,则x,z比较结果和y,z比较结果相同。异常
反例:下例中没有处理相等的状况,实际使用中可能会出现异常:
sort
new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return o1.getId() > o2.getId() ? 1 : -1; } }