2 回答
TA贡献1829条经验 获得超7个赞
这是我很快想到的一个选项:
Collections.sort(markings, (o1, o2) -> {
int value = o1.getName().compareToIgnoreCase(o2.getName());
if(value == 0) {
return o1.getOldName().compareToIgnoreCase(o2.getOldName());
}
if(o1.getName().equals("-")) {
return 1;
}
if(o2.getName().equals("-")) {
return -1;
}
return value;
});
TA贡献2041条经验 获得超4个赞
这是一个建议。顺序很好,我们所做的就是取出所有以“-”开头的元素并将它们从列表中删除,然后将它们添加回末尾。这是低效的,但它是一种决心。
public static void main(String[] args) {
List<Marking> markings = new ArrayList<>();
markings.add(new Marking("-", "Limited ETS Joint Procurement"));
markings.add(new Marking("-", "EU Satellite Navigation matters"));
markings.add(new Marking("SENSITIVE: Court procedural documents", "Court procedural documents"));
markings.add(new Marking("SENSITIVE: COMP Operations", "COMP Operations"));
markings.add(new Marking("-", "ETS Limited"));
markings.add(new Marking("SENSITIVE: Staff matter", "Staff Matter"));
markings.add(new Marking("SPECIAL HANDLING: ETS Critical", "ETS Critical"));
markings.sort((o1, o2) -> {
int value = o1.getName().compareToIgnoreCase(o2.getName());
if (value == 0) {
return o1.getOldName().compareToIgnoreCase(o2.getOldName());
}
return value;
});
List<Marking> dashed = markings.stream().filter(marking -> marking.name.startsWith("-") || marking.oldName.startsWith("-"))
.collect(Collectors.toList());
markings.removeAll(dashed);
markings.addAll(dashed);
for (Marking marking : markings) {
System.out.println(marking.getName() + ": " + marking.getOldName());
}
}
添加回答
举报