1. 基本数组:
//直接通过Arrays.sort工具类
int[] arr = new int[]{1,2,6,24,5,68,9,0}; Arrays.sort(arr);
2.对象数组:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | //通过实现Comparable接口来排序public class student implements Comparable<student> { String name; int age; public student(String name, int age) { this.name = name; this.age = age; } @Override public int compareTo(student o) {// return this.age-o.age;//升序 return o.age- this.age;//降序 }} student[] arr = new student[]{ new student("a",4), new student("a",7),new student("a",1)}; Arrays.sort(arr) //实现Comparator接口来排序 public class student { String name; int age; int score; public student(String name, int age, int score) { this .name = name; this .age = age; this .score = score; } @Override public String toString() { return "student{" + "name='" + name + '\ '' + ", age=" + age + ", score=" + score + '}' ; } } student[] arr = new student[]{ new student( "a" , 4 , 100 ), new student( "b" , 9 , 100 ), new student( "c" , 7 , 80 ), new student( "d" , 20 , 100 ), new student( "f" , 11 , 50 ), }; Arrays.sort(arr,(o1, o2) -> { //这里比较的可以是多个 ,name长度比较, name直接自然顺序比较,代码显示的是成绩比较和年级比较 int num = o2.score-o1.score; //成绩降序 int num1 = num == 0 ?o1.age-o2.age:num; // 若果成绩相等的话就判断年龄 年龄升序 ; return num1; }); for (student student : arr) { System.out.println(student); } |
结果显示:
3.list排序
//简单的Integer类型
//直接用 Colletions工具类
ArrayList<Integer> list = new ArrayList<>(); list.add(9); list.add(3); list.add(5); list.add(1); Collections.sort(list); System.out.println(list);
结果:[1, 3, 5, 9]
//list存储学生对象排序 public class student { String name; int age; int score; public student(String name, int age, int score) { this.name = name; this.age = age; this.score = score; } @Override public String toString() { return "student{" + "name='" + name + '\'' + ", age=" + age + ", score=" + score + '}'; } } ArrayList<student> list = new ArrayList<>(); list.add( new student("a",4,100)); list.add(new student("b",9,100)); list.add(new student("d",20,100)); list.add(new student("c",7,80)); list.add(new student("f",11,50)); Collections.sort(list,(o1, o2) ->{ //这里比较的可以是多个 ,name长度比较, name直接自然顺序比较,代码显示的是成绩比较和年级比较 int num = o2.score-o1.score;//成绩降序 int num1 = num == 0?o1.age-o2.age:num;// 若果成绩相等的话就判断年龄 年龄升序 ; return num1; } ); System.out.println(list); } //结果[student{name='a', age=4, score=100}, // student{name='b', age=9, score=100}, // student{name='d', age=20, score=100}, // student{name='c', age=7, score=80}, // student{name='f', age=11, score=50}]
4.set排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | public class student implements Comparable<student> { String name; int age; int score; public student(String name, int age, int score) { this .name = name; this .age = age; this .score = score; } @Override public String toString() { return "student{" + "name='" + name + '\ '' + ", age=" + age + ", score=" + score + '}' ; } @Override public int compareTo(student o) { return this .score-o.score; //score升序 } } /HashSet它存储无序,离散,所以无法直接排序,只能间接排序了. 介绍两种办法 , 1 转成List进行排序 2 转成TreeSet进行排序 //list前面已经排序过,这里就不展示了,这里就展示转换成ThreeSet再排序,看代码; HashSet<student> hashSet = new HashSet<>(); hashSet.add( new student( "貂蝉" , 42 , 100 )); hashSet.add( new student( "后裔" , 43 , 100 )); hashSet.add( new student( "孙悟空" , 4 , 35 )); hashSet.add( new student( "阿珂" , 7 , 58 )); hashSet.add( new student( "妲己" , 22 , 80 )); TreeSet<student> set = new TreeSet<>((o1, o2) -> { //排序用Comparable或者Comparator Comparable直接操作一个类,这里就不演示了, 直接上Comparator操作对象这个简便的 //这里比较的可以是多个 ,name长度比较, name直接自然顺序比较,代码显示的是成绩比较和年级比较 int num = o2.score-o1.score; //成绩降序 int num1 = num == 0 ?o1.age-o2.age:num; // 若果成绩相等的话就判断年龄 年龄升序 ; return num1; }); //交换元素 for (student student : hashSet) { set.add(student); } System.out.println(set); //结果[student{name='貂蝉',age=42, score=100}, // student{name='后裔', age=43, score=100}, // student{name='妲己', age=22, score=80}, // student{name='阿珂', age=7, score=58}, // student{name='孙悟空', age=4, score=35}] } |
5.map排序 看这里 能力有限 写不下去了 学的越多 你会发现 你知道的越少 调整心态才是重要的
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!