根据list里面对象的某个特定属性排序,若属性值为空的处理
Collections.sort(fileRecordDOList, Comparator.comparing(FileRecordDO::getOperateTime).reversed());
reversed()逆序排序,从大到小
若排序的属性值有空值,以上方法会报错,需要使用以下方法
升序:从小到大排序
1 2 Collections.sort(userList, new Comparator<User>() { 3 @Override 4 public int compare(User o1, User o2) { 5 //若有空值可以在这里赋值 6 return o1.getName().compareTo(o2.getName()); 7 } 8 });
降序:从大到小排序
1 Collections.sort(userList, new Comparator<User>() { 2 @Override 3 public int compare(User o1, User o2) { 4 //若有空值可以在这里赋值 5 return o2.getName().compareTo(o1.getName()); 6 } 7 });
多条件排序
1 Collections.sort(recentModifyPredictLogDTOList, new Comparator<RecentModifyPredictLogDTO>() { 2 @Override 3 public int compare(RecentModifyPredictLogDTO o1, RecentModifyPredictLogDTO o2) { 4 if (o2.getPredictUpdateCount().compareTo(o1.getPredictUpdateCount()) == 1) { 5 return 1; 6 } else if (o2.getPredictUpdateCount().compareTo(o1.getPredictUpdateCount()) == -1) { 7 return -1; 8 } else if (o2.getPredictUpdateCount().compareTo(o1.getPredictUpdateCount()) == 0) { 9 // 如果成交套数相等的话就按照响应速度升序 10 if (o2.getPredictRealName().compareTo(o1.getPredictRealName()) == -1) { 11 return 1; 12 } else if (o2.getPredictRealName().compareTo(o1.getPredictRealName()) == 1) { 13 return -1; 14 } else if (o2.getPredictRealName().compareTo(o1.getPredictRealName()) == 0) { 15 if (o2.getStockShortName().compareTo(o1.getStockShortName()) == -1) { 16 return 1; 17 } else if (o2.getStockShortName().compareTo(o1.getStockShortName()) == 1) { 18 return -1; 19 } else if (o2.getStockShortName().compareTo(o1.getStockShortName()) == 0) { 20 return 0; 21 } 22 } 23 } 24 return 0; 25 } 26 });