List排序問題:根據list物件的建立時間排序,後用lambda表示式
阿新 • • 發佈:2019-02-05
List<ResponseClient> responseClientListSelectedList = new ArrayList<>();
Collections.sort(responseClientListSelectedList, new Comparator<ResponseClient>() { @Override public int compare(ResponseClient o1, ResponseClient o2) { return 0; } });
對List排序:常用的內部類實現比較器,但是是對日期排序,最初:
用int強轉回丟失精度,所以用日期的比較方法:Collections.sort(responseClientListSelectedList, new Comparator<ResponseClient>() { @Override public int compare(ResponseClient o1, ResponseClient o2) { return (int)(o1.getCreateTime().getTime() - o2.getCreateTime().getTime()); } });
最後使用lambda表示式改:Collections.sort(responseClientListSelectedList, new Comparator<ResponseClient>() { @Override public int compare(ResponseClient o1, ResponseClient o2) { return o1.getCreateTime().compareTo(o2.getCreateTime()); } });
// 時間排序
Collections.sort(responseClientListSelectedList, (o1, o2) -> (o2.getCreateTime().compareTo(o1.getCreateTime()))
);