我需要订购一个对象列表,首先按日期排序,然后按名称按日期排序..像下面的示例通过比较器:
- 19/03/2014 Anna Hatta
- 19/03/2014 Keller Jay
- 20/03/2014 Anna Hatta
- 20/03/2014 Keller Jay
- 20/03/2014 Zoner Charley
我知道如何分别按日期和名称排序,例如:
public static Comparator<Prospect> DATE_ORDER_PROSPECTS = new Comparator<Prospect>() {
public int compare(Prospect p1, Prospect p2) {
int res = p2.getDate().compareTo(p1.getDate());
return res;
}
};
但是,如何结合这两个并进行嵌套排序?
干杯!
最佳答案
public static Comparator<Prospect> DATE_NAME_PROSPECTS = new Comparator<Prospect>() {
public int compare(Prospect p1, Prospect p2) {
int res = p1.getDate().compareTo(p2.getDate());
return res == 0 ? p1.getName().compareTo(p2.getName()) : res;
}
};