在提供的初始排序代码中,比较中字段的串联使得分离字段进行排序变得具有挑战性。要纠正此问题,请考虑在字段之间添加空格。或者,您可以探索以下替代方案:
Collections.sort(reportList, Comparator.comparing(Report::getReportKey)
.thenComparing(Report::getStudentNumber) .thenComparing(Report::getSchool));
ComparatorChain chain = new ComparatorChain(Arrays.asList(
new BeanComparator ("size"),
new BeanComparator("nrOfToppings"),
new BeanComparator("name")));
Collections.sort(披萨,链);
Collections.sort(pizzas, new Comparator
@Override public int compare(Pizza p1, Pizza p2) { return ComparisonChain.start().compare(p1.size, p2.size).compare(p1.nrOfToppings, p2.nrOfToppings).compare(p1.name, p2.name).result(); }
})
Collections.sort(pizzas, new Comparator
@Override public int compare(Pizza p1, Pizza p2) { return new CompareToBuilder().append(p1.size, p2.size).append(p1.nrOfToppings, p2.nrOfToppings).append(p1.name, p2.name).toComparison(); }
} );
以上是如何在 Java 中对具有多个字段的对象列表进行排序?的详细内容。更多信息请关注PHP中文网其他相关文章!