美文网首页
list列表重新排序 list.sort(Comparator.

list列表重新排序 list.sort(Comparator.

作者: IT男的假智慧 | 来源:发表于2019-12-23 10:59 被阅读0次

第一种java8以上使用:list.sort(Comparator.comparing(CommunityResponse::getCreatedDate).reversed());

第二种办法:

private static void ListSort(List<CommunityResponse> list) {

     Collections.sort(list, new Comparator<CommunityResponse>() { 

 @Override 

 public int compare(CommunityResponse o1, CommunityResponse o2) {

 try { Date dt1 = o1.getCreatedDate(); Date dt2 = o2.getCreatedDate();

 if (dt1.getTime() > dt2.getTime()) 

{ return 1; }

 else if (dt1.getTime() < dt2.getTime()) 

{ return -1; } else { return 0; }

 } catch (Exception e) {

 e.printStackTrace(); } return 0; 

 } });

}

相关文章

网友评论

      本文标题:list列表重新排序 list.sort(Comparator.

      本文链接:https://www.haomeiwen.com/subject/gukznctx.html