<dependency> <groupId>com.google.collections</groupId> <artifactId>google-collections</artifactId> <version>1.0</version> </dependency> import com.google.common.collect.Ordering; public class Student { private String name; private int score; public Student(String name, int score) { this.name = name; this.score = score; } public String getName() { return name; } public int getScore() { return score; } } public class Main { public static void main(String[] args) { List<Student> students = new ArrayList<>(); students.add(new Student("Alice", 80)); students.add(new Student("Bob", 90)); students.add(new Student("Charlie", 70)); Ordering<Student> byScoreOrdering = Ordering.natural().onResultOf(Student::getScore); List<Student> sortedStudents = byScoreOrdering.sortedCopy(students); for (Student student : sortedStudents) { System.out.println(student.getName() + ": " + student.getScore()); } } }


上一篇:
下一篇:
切换中文