自定义排序Comparator
// 定义一个方法,该方法接收泛型类型T并返回泛型类型U的函数
public static <T, U> Comparator<T> myComparing(Function<? super T, ? extends U> keyExtractor) {
return (o1, o2) -> {
String key1 = String.valueOf(keyExtractor.apply(o1));
String key2 = String.valueOf(keyExtractor.apply(o2));
if(StringUtils.isBlank(key2)){
return -1;
}
if(StringUtils.isBlank(key1)){
return 1;
}
double a=0;
try {
a=Double.parseDouble(key1);
} catch (NumberFormatException e) {
return 1;
}
double b=0;
try {
b=Double.parseDouble(key2);
} catch (NumberFormatException e) {
return -1;
}
return Double.compare(a,b);
};
}
Comparator<GetDataDetailsVo> comparable= myComparing(GetDataDetailsVo::getOtHours);
switch (otLeaveReportQueryVO.getField()){
case 1:
comparable= Comparator.comparing(GetDataDetailsVo::getLos);
break;
case 2:
comparable= Comparator.comparing(GetDataDetailsVo::getGroup);
break;
case 3:
comparable= Comparator.comparing(GetDataDetailsVo::getTeam);
break;
case 4:
comparable= Comparator.comparing(GetDataDetailsVo::getStaffId);
break;
case 5:
comparable= Comparator.comparing(GetDataDetailsVo::getNotesName);
break;
case 6:
comparable= myComparing(GetDataDetailsVo::getOtHours);
break;
case 7:
comparable= myComparing(GetDataDetailsVo::getOtTimes);
break;
case 8:
comparable= myComparing(GetDataDetailsVo::getTimesOfThreeHours);
break;
case 9:
break;
case 10:
comparable= myComparing(GetDataDetailsVo::getStandardWorkingDay);
break;
case 11:
if(YTD.equals(otLeaveReportQueryVO.getDimension())){
comparable= myComparing(GetDataDetailsVo::getActualChargeableUtYtd);
}else if (MTD.equals(otLeaveReportQueryVO.getDimension())){
comparable= myComparing(GetDataDetailsVo::getActualChargeableUtMtd);
}
break;
case 12:
comparable= myComparing(GetDataDetailsVo::getAvgOtPerTime);
break;
case 13:
comparable= myComparing(GetDataDetailsVo::getAvgOtPerDay);
break;
default:
comparable= myComparing(GetDataDetailsVo::getOtHours);
}
if(!isAsc){
comparable=comparable.reversed();
}
List<GetDataDetailsVo> dataDetailsVoList=newDataDetailsVoList.stream().sorted(comparable).collect(Collectors.toList());