From 2769b710a632db17a3fbc0b33e8373064091398b Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Thu, 15 Oct 2026 10:24:22 +0800 Subject: [PATCH] =?UTF-8?q?s-day03--=E6=AF=94=E8=BE=83=E5=99=A8Comparetor?= =?UTF-8?q?=E7=9A=84=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/inmind/colletions04/Demo14.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/s-day03/src/com/inmind/colletions04/Demo14.java b/s-day03/src/com/inmind/colletions04/Demo14.java index 2c6b9ef..a0af93f 100644 --- a/s-day03/src/com/inmind/colletions04/Demo14.java +++ b/s-day03/src/com/inmind/colletions04/Demo14.java @@ -31,5 +31,25 @@ public class Demo14 { }); System.out.println(lists); //请大家创建一个保存学生对象的集合,并按学生的年龄升序排序,要求使用比较器 + ArrayList students = new ArrayList<>(); + Collections.addAll(students + , new Student("张三1", 19) + , new Student("张三2", 20) + , new Student("张三3", 18) + , new Student("张三4", 21) + ); + System.out.println(students); + //希望集合的学生按年龄排序(升序,降序) + Collections.sort(students); + System.out.println(students); + //使用比较器对学生,按年龄升序排序(比较器) + Collections.sort(students, new Comparator() { + @Override + public int compare(Student o1, Student o2) { + return o1.age-o2.age; + } + }); + + System.out.println(students); } }