1. 程式人生 > >List排序問題:根據list物件的建立時間排序,後用lambda表示式

List排序問題:根據list物件的建立時間排序,後用lambda表示式

List<ResponseClient> responseClientListSelectedList = new ArrayList<>();

Collections.sort(responseClientListSelectedList, new Comparator<ResponseClient>() { @Override public int compare(ResponseClient o1, ResponseClient o2) { return 0; } });

對List排序:常用的內部類實現比較器,但是是對日期排序,最初:

Collections.sort(responseClientListSelectedList, new Comparator<ResponseClient>() {
            @Override
            public int compare(ResponseClient o1, ResponseClient o2) {
                return (int)(o1.getCreateTime().getTime() - o2.getCreateTime().getTime());
            }
        });
用int強轉回丟失精度,所以用日期的比較方法:
 Collections.sort(responseClientListSelectedList, new Comparator<ResponseClient>() {
            @Override
            public int compare(ResponseClient o1, ResponseClient o2) {
                return o1.getCreateTime().compareTo(o2.getCreateTime());
            }
        });
最後使用lambda表示式改:
// 時間排序
        Collections.sort(responseClientListSelectedList, (o1, o2) -> (o2.getCreateTime().compareTo(o1.getCreateTime()))
        );