3 回答
TA贡献1848条经验 获得超10个赞
public static class MyObject implements Comparable<MyObject> { private Date dateTime; public Date getDateTime() { return dateTime; } public void setDateTime(Date datetime) { this.dateTime = datetime; } @Override public int compareTo(MyObject o) { return getDateTime().compareTo(o.getDateTime()); }}
Collections.sort(myList);
Collections.sort(myList, new Comparator<MyObject>() { public int compare(MyObject o1, MyObject o2) { return o1.getDateTime().compareTo(o2.getDateTime()); }});
public static class MyObject implements Comparable<MyObject> { private Date dateTime; public Date getDateTime() { return dateTime; } public void setDateTime(Date datetime) { this.dateTime = datetime; } @Override public int compareTo(MyObject o) { if (getDateTime() == null || o.getDateTime() == null) return 0; return getDateTime().compareTo(o.getDateTime()); }}
Collections.sort(myList, new Comparator<MyObject>() { public int compare(MyObject o1, MyObject o2) { if (o1.getDateTime() == null || o2.getDateTime() == null) return 0; return o1.getDateTime().compareTo(o2.getDateTime()); }});
TA贡献1854条经验 获得超8个赞
// sort DateTime typed list
list.sort((d1,d2) -> d1.compareTo(d2));
// or an object which has an DateTime attribute
list.sort((o1,o2) -> o1.getDateTime().compareTo(o2.getDateTime()));
// or like mentioned by Tunaki
list.sort(Comparator.comparing(o -> o.getDateTime()))
TA贡献2036条经验 获得超8个赞
Collections.sort(myList, new Comparator<MyObject> { public int compare(MyObject o1, MyObject o2) { DateTime a = o1.getDateTime(); DateTime b = o2.getDateTime(); if (a.lt(b)) return -1; else if (a.lteq(b)) // it's equals return 0; else return 1; }});
添加回答
举报