在提供的初始排序程式碼中,比較中欄位的串聯使得分離字段進行排序變得具有挑戰性。若要修正此問題,請考慮在欄位之間新增空格。或者,您可以探索以下替代方案:
Collections.sort(reportList, Comparator.comparing(Report::getReportKey)
.thenComparing(Report::getStudentNumber) .thenComparing(Report::getSchool));
ComparatorChain chain = new ComparatorChain(Arrays.asList(
new BeanAter 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. , 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中文網其他相關文章!