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排序 看這里 能力有限 寫不下去了 學的越多 你會發現 你知道的越少 調整心態才是重要的