使用匿名函数的方式对result数据进行排序:
Collections.sort(result, new Comparator<WorkerVO>() {
@Override
public int compare(WorkerVO o1, WorkerVO o2) {
int serviceCount1 = o1.getServiceCount();
int serviceCount2 = o2.getServiceCount();
long restMinutes1 = o1.getRestMinutes();
long restMinutes2 = o2.getRestMinutes();
if (0 == rankTyepe) {//服务人数降序
int compare = Integer.compare(serviceCount2, serviceCount1);
return compare==0?Long.compare(restMinutes1, restMinutes2):compare;
} else if (1 == rankTyepe) { //服务人数升序
int compare = Integer.compare(serviceCount1, serviceCount2);
return compare==0?Long.compare(restMinutes2, restMinutes1):compare; `//表示,如果服务人数相等就按空闲时间排序(多条件排序的应用和)`
}
return 0;
}
});
java8:对list排序
list.sort((a, b) -> Integer.compare(a.age, b.getAge()));
assignWorkerDOList.sort((a, b) -> Long.compare(a.getServiceStartDate().getTime(), b.getServiceStartDate().getTime()));
//日期字段进行排序 注:排序不作用于原集合,要重新返回结果
List<AttendantAssignDetailDTO> collect = result.stream().sorted(Comparator.comparing(AttendantAssignDetailDTO::getServiceStartDate).reversed()).collect(Collectors.toList());
参考:https://blog.csdn.net/j277699931/article/details/49660607