1、写作背景
总想走捷径的人,往往因找不到捷径而固步自封,一步不前!
2、参考网址
3、学习目的
- 要考虑线程并发
- 场景一:单字段排序(根据前端传递的字段进行排序)
- 在类中写
- 直接简写
- 场景二:多字段排序(固定使用前端传递的字段顺序进行排序)
4、核心操作
1)场景一[类写]
场景一:单字段排序(根据前端传递的字段进行排序)
@Override
public int compareTo(Scene1Model model) {
Map<String, String> sortMap = Scene1Model.getSortMap().get();
if (MapUtils.isNotEmpty(sortMap)) {
String sortField = sortMap.get(AppConstants.ORDER_FIELD);
String sortType = sortMap.get(AppConstants.ORDER_TYPE);
int sortIntValue = CompareSortUtil.getSortIntValue(sortType);
if (StringUtils.equalsIgnoreCase(sortField, AppConstants.ORDER_CLK_NUM) &&
this.getClkNum() != model.getClkNum()) {
return sortIntValue * this.getClkNum().compareTo(model.getClkNum());
} else if (StringUtils.equalsIgnoreCase(sortField, AppConstants.ORDER_USR_NUM) &&
this.getUsrNum() != model.getUsrNum()) {
return sortIntValue * this.getUsrNum().compareTo(model.getUsrNum());
} else if (StringUtils.equalsIgnoreCase(sortField, AppConstants.ORDER_USR_ID) &&
this.getUsrId() != model.getUsrId()) {
return sortIntValue * this.getUsrId().compareTo(model.getUsrId());
}
}
return this.getUsrId().compareTo(model.getUsrId());
}
2)场景一[简写]
场景二:多字段排序(固定使用前端传递的字段顺序进行排序)
List<Scene1SimpleModel> scene1Models = Scene1SimpleTest.initDataList();
Map<String, String> sortMap = new HashMap<>();
sortMap.put(AppConstants.ORDER_FIELD, AppConstants.ORDER_USR_NUM);
sortMap.put(AppConstants.ORDER_TYPE, AppConstants.ORDER_TYPE_DESC);
// 进行排序简写
Collections.sort(scene1Models, new Comparator<Scene1SimpleModel>() {
@Override
public int compare(Scene1SimpleModel o1, Scene1SimpleModel o2) {
// sortMap直接从上面的参数中传递过
if (MapUtils.isNotEmpty(sortMap)) {
String sortField = sortMap.get(AppConstants.ORDER_FIELD);
String sortType = sortMap.get(AppConstants.ORDER_TYPE);
int sortIntValue = CompareSortUtil.getSortIntValue(sortType);
if (StringUtils.equalsIgnoreCase(sortField, AppConstants.ORDER_CLK_NUM) &&
o1.getClkNum() != o2.getClkNum()) {
return sortIntValue * o1.getClkNum().compareTo(o2.getClkNum());
} else if (StringUtils.equalsIgnoreCase(sortField, AppConstants.ORDER_USR_NUM) &&
o1.getUsrNum() != o2.getUsrNum()) {
return sortIntValue * o1.getUsrNum().compareTo(o2.getUsrNum());
} else if (StringUtils.equalsIgnoreCase(sortField, AppConstants.ORDER_USR_ID) &&
o1.getUsrId() != o2.getUsrId()) {
return sortIntValue * o1.getUsrId().compareTo(o2.getUsrId());
}
}
return o1.getUsrId().compareTo(o2.getUsrId());
}
});
CompareSortUtil.printDataList(scene1Models);
3)场景二
场景二:多字段排序(固定使用前端传递的字段顺序进行排序)
@Override
public int compareTo(Scene2Model model) {
Map<String, String> sortMap = Scene2Model.getSortMap().get();
if (MapUtils.isNotEmpty(sortMap)) {
String clkNumSort = sortMap.get(AppConstants.ORDER_CLK_NUM);
String usrNumSort = sortMap.get(AppConstants.ORDER_USR_NUM);
String usrIdSort = sortMap.get(AppConstants.ORDER_USR_ID);
if (this.getClkNum() != model.getClkNum()) {
return CompareSortUtil.getSortIntValue(clkNumSort) * this.getClkNum().compareTo(model.getClkNum());
} else if (this.getUsrNum() != model.getUsrNum()) {
return CompareSortUtil.getSortIntValue(usrNumSort) * this.getUsrNum().compareTo(model.getUsrNum());
} else if (this.getUsrId() != model.getUsrId()) {
return CompareSortUtil.getSortIntValue(usrIdSort) * this.getUsrId().compareTo(model.getUsrId());
}
}
return this.getUsrId().compareTo(model.getUsrId());
}
网友评论