楼主,你代码那边的三目运算符那样写有问题的,还有定义比较器的时候,最好是重新再定义一个类去实现comparator接口,这样的代码会比较好维护.- package cn.test;
- import java.util.Comparator;
- public class ComparatorByAge implements Comparator {
- public int compare(Object o1, Object o2) {
- Student s1 = (Student)o1;
- Student s2 = (Student)o2;
- int temp = s1.getAge() - s2.getAge();
- return temp == 0?s1.getName().compareTo(s2.getName()):temp;
- }
- }
复制代码 |