private void setListOrder(List<String> orderRegulation, List<OvermindFieldConfig> targetList) {
//按照字段名来排序
Collections.sort(targetList, ((o1, o2) -> {
int io1 = orderRegulation.indexOf(o1.getName());
int io2 = orderRegulation.indexOf(o2.getName());
if (io1 != -1) {
io1 = targetList.size() - io1;
}
if (io2 != -1) {
io2 = targetList.size() - io2;
}
return io2 - io1;
}));
}
一个List相对另一个List进行排序
最新推荐文章于 2023-02-28 14:19:47 发布
这段代码展示了如何使用Collections.sort方法对一个OvermindFieldConfig对象列表进行排序,排序依据是另一个包含字段名的字符串列表orderRegulation。通过比较orderRegulation中元素的索引,实现了目标列表的定制排序。如果在orderRegulation中找不到对象的字段名,则将其位置设为目标列表的末尾。
摘要由CSDN通过智能技术生成