1.原始comparator
java
public static List<ReportVehicleVo> sortByBuildingPhaseAndFdeName(List<ReportVehicleVo> list){
list.sort(new Comparator<ReportVehicleVo>() {
@Override
public int compare(ReportVehicleVo o1, ReportVehicleVo o2) {
if (StringUtils.isBlank(o1.getVeBuildingPhase())){
return -1;
}
if (StringUtils.isBlank(o2.getVeBuildingPhase())){
return 1;
}
if (o1.getVeBuildingPhase().compareTo(o2.getVeBuildingPhase()) > 0){
return 1;
}else if (o1.getVeBuildingPhase().compareTo(o2.getVeBuildingPhase()) < 0){
return -1;
}else {
if (StringUtils.isBlank(o1.getFdeName())){
return -1;
}
if (StringUtils.isBlank(o2.getFdeName())){
return 1;
}
if (o1.getFdeName().compareTo(o2.getFdeName()) > 0){
return 1;
}else {
return -1;
}
}
}
});
return list;
}
原始方法判空后再排序
2.优雅方式
java
List<String> testingPlaceList = new ArrayList<>();
testingPlaceList.sort(Comparator.nullsFirst(Comparator.naturalOrder()));
下边一行代码执行完就排好序了,并且nullsFirst会把null值排到前边,如果你想排到后边就换成nullsLast,另外naturalOrder是正序,如果你想倒序就换成reverseOrder