public static void main(String[] args) {
List<String> list = Arrays.asList("小明", "小张", "小王", "小李", "小刘");
List<String> list1 = Arrays.asList("小学生", "中学生", "高中生", "大学生", "打工人", "老板");
ElementMoreVO element1 = new ElementMoreVO();
element1.setStatus("小张");
element1.setSource("高中生");
ElementMoreVO element2 = new ElementMoreVO();
element2.setStatus("小明");
element2.setSource("中学生");
ElementMoreVO element3 = new ElementMoreVO();
element3.setStatus("小王");
element3.setSource("中学生");
ElementMoreVO element4 = new ElementMoreVO();
element4.setStatus("小李");
element4.setSource("小学生");
ElementMoreVO element5 = new ElementMoreVO();
element5.setStatus("小刘");
element5.setSource("大学生");
ElementMoreVO element6 = new ElementMoreVO();
element6.setStatus("小王");
element6.setSource("老板");
ElementMoreVO element7 = new ElementMoreVO();
element7.setStatus("小张");
element7.setSource("打工人");
List<ElementMoreVO> elementList = new ArrayList<>();
elementList.add(element1);
elementList.add(element2);
elementList.add(element3);
elementList.add(element4);
elementList.add(element5);
elementList.add(element6);
elementList.add(element7);
sort(list, list1, elementList);
System.out.println(JSON.toJSONString(elementList));
}
/**
* 一个集合按照指定的顺序(另外两个集合)进行排序(排序规则1相同的按照排序规则2排序)
*
* @param list 排序规则1
* @param list1 排序规则2
* @param elementList 排序对象集合
*/
private static void sort(List<String> list, List<String> list1, List<ElementMoreVO> elementList) {
if (CollectionUtils.isEmpty(list) && CollectionUtils.isEmpty(elementList)) {
return;
}
Collections.sort(elementList, ((o1, o2) -> {
int io1 = list.indexOf(o1.getStatus());
int io2 = list.indexOf(o2.getStatus());
int jo1 = list1.indexOf(o1.getSource());
int jo2 = list1.indexOf(o2.getSource());
if (-1 != io1) {
io1 = elementList.size() - io1;
}
if (-1 != io2) {
io2 = elementList.size() - io2;
}
if (io1 == io2) {
return jo1 - jo2;
}
return io2 - io1;
}));
}
Collections.sort 按照自定义的固定顺序排序
最新推荐文章于 2024-08-13 09:54:51 发布