Collections.sort(employees, new Comparator<Employee>() {
public int compare(Employee s, Employee s1) {
int comp = s.getName().compareTo(s1.getName());
if (comp != 0) { // names are different
return comp;
}
return s.getSalary() - s1.getSalary();
}
});
System.out.println(employees);
}
Run Code Online (Sandbox Code Playgroud)
使用BigDecimal.compareTo(BigDecimal)已经为您完成所有逻辑的操作:
return s.getSalary().compareTo(s1.getSalary());
Run Code Online (Sandbox Code Playgroud)
的-(负)操作者只对原始的数字和它们的专用包装类型定义。
| 归档时间: |
|
| 查看次数: |
240 次 |
| 最近记录: |