关于Comparable 与Comparator的问题
public int compare(Employee o1, Employee o2) {
Employee em1 = (Employee)o1;
Employee em2 = (Employee)o2;
if(em1.getSalary() != em2.getSalary())
return (int)(em2.getSalary()-em1.getSalary());
if(em1.getAge()!=em2.getAge())
return (int)(em2.getAge()-em1.getAge());
return em1.getName().compareTo(em2.getName());
}
为什么用Comparator能够实现( salary高的在前面,salary相同时age大的在前面,age也相同时按照name升序排列)
而同样的代码用Comparable(就只是salary实现了,但是age依然是升序排序。