1. 基本数组:
//直接通过Arrays.sort工具类
int[] arr = new int[]{1,2,6,24,5,68,9,0}; Arrays.sort(arr);
2.对象数组:
//通过实现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排序
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排序 看这里 能力有限 写不下去了 学的越多 你会发现 你知道的越少 调整心态才是重要的