Java ArrayList的自定義排序

Java中實現對list的自定義排序主要通過兩種方式

1)讓需要進行排序的對象的類實現Comparable接口,重寫compareTo(T o)方法,在其中定義排序規則,那麼就可以直接調用Collections.sort()來排序對象數組

[java] view plain copy
  1. public class Student implements Comparable{  
  2.     private int id;  
  3.     private int age;  
  4.     private int height;  
  5.     private String name;  
  6.   
  7.     public Student(int id, String name, int age, int height) {  
  8.         this.id = id;  
  9.         this.name = name;  
  10.         this.age = age;  
  11.         this.height = height;  
  12.     }  
  13.   
  14.     public int getId() {  
  15.         return id;  
  16.     }  
  17.   
  18.     public int getAge() {  
  19.         return age;  
  20.     }  
  21.   
  22.     public int getHeight() {  
  23.         return height;  
  24.     }  
  25.   
  26.     public String getName() {  
  27.         return name;  
  28.     }  
  29.   
  30.     public void setId(int id) {  
  31.         this.id = id;  
  32.     }  
  33.   
  34.     public void setAge(int age) {  
  35.         this.age = age;  
  36.     }  
  37.   
  38.     public void setName(String name) {  
  39.         this.name = name;  
  40.     }  
  41.   
  42.     public void setHeight(int height) {  
  43.         this.height = height;  
  44.     }  
  45.   
  46.     @Override  
  47.     public int compareTo(Object o) {  
  48.         Student s = (Student) o;  
  49.         if (this.age > s.age) {  
  50.             return 1;  
  51.         }  
  52.         else if (this.age < s.age) {  
  53.             return -1;  
  54.         }  
  55.         else {  
  56.             if (this.height >= s.height) {  
  57.                 return 1;  
  58.             }  
  59.             else {  
  60.                 return -1;  
  61.             }  
  62.         }  
  63.     }  
  64. }  

測試類:

[java] view plain copy
  1. import java.util.*;  
  2.   
  3. public class Test {  
  4.   
  5.     public static void printData(List<Student> list) {  
  6.         for (Student student : list) {  
  7.             System.out.println("學號:" + student.getId() + " 姓名:" + student.getName() + " 年齡" + student.getAge() + " 身高:" + student.getHeight());  
  8.         }  
  9.     }  
  10.   
  11.     public static void main(String[] args) {  
  12.         List<Student> list = new ArrayList<>();  
  13.         list.add(new Student(1"A"20180));  
  14.         list.add(new Student(2"B"21175));  
  15.         list.add(new Student(3"C"22190));  
  16.         list.add(new Student(4"D"21170));  
  17.         list.add(new Student(5"E"20185));  
  18.         System.out.println("before sorted");  
  19.         printData(list);  
  20.         Collections.sort(list);  
  21.         System.out.println("after age and height sorted");  
  22.         printData(list);  
  23.     }  
  24. }  

結果:

[plain] view plain copy
  1. before sorted  
  2. 學號:1 姓名:A 年齡20 身高:180  
  3. 學號:2 姓名:B 年齡21 身高:175  
  4. 學號:3 姓名:C 年齡22 身高:190  
  5. 學號:4 姓名:D 年齡21 身高:170  
  6. 學號:5 姓名:E 年齡20 身高:185  
  7. after age and height sorted  
  8. 學號:1 姓名:A 年齡20 身高:180  
  9. 學號:5 姓名:E 年齡20 身高:185  
  10. 學號:4 姓名:D 年齡21 身高:170  
  11. 學號:2 姓名:B 年齡21 身高:175  
  12. 學號:3 姓名:C 年齡22 身高:190  

2)實現比較器接口Comparator,重寫compare方法,直接當做參數傳進sort中

[java] view plain copy
  1. public class Student {  
  2.     private int id;  
  3.     private int age;  
  4.     private int height;  
  5.     private String name;  
  6.   
  7.     public Student(int id, String name, int age, int height) {  
  8.         this.id = id;  
  9.         this.name = name;  
  10.         this.age = age;  
  11.         this.height = height;  
  12.     }  
  13.   
  14.     public int getId() {  
  15.         return id;  
  16.     }  
  17.   
  18.     public int getAge() {  
  19.         return age;  
  20.     }  
  21.   
  22.     public int getHeight() {  
  23.         return height;  
  24.     }  
  25.   
  26.     public String getName() {  
  27.         return name;  
  28.     }  
  29.   
  30.     public void setId(int id) {  
  31.         this.id = id;  
  32.     }  
  33.   
  34.     public void setAge(int age) {  
  35.         this.age = age;  
  36.     }  
  37.   
  38.     public void setName(String name) {  
  39.         this.name = name;  
  40.     }  
  41.   
  42.     public void setHeight(int height) {  
  43.         this.height = height;  
  44.     }  
  45. }  

測試類:

[java] view plain copy
  1. import java.util.*;  
  2.   
  3. public class Test {  
  4.   
  5.     public static void printData(List<Student> list) {  
  6.         for (Student student : list) {  
  7.             System.out.println("學號:" + student.getId() + " 姓名:" + student.getName() + " 年齡" + student.getAge() + " 身高:" + student.getHeight());  
  8.         }  
  9.     }  
  10.   
  11.     public static void main(String[] args) {  
  12.         List<Student> list = new ArrayList<>();  
  13.         list.add(new Student(1"A"20180));  
  14.         list.add(new Student(2"B"21175));  
  15.         list.add(new Student(3"C"22190));  
  16.         list.add(new Student(4"D"21170));  
  17.         list.add(new Student(5"E"20185));  
  18.         System.out.println("before sorted");  
  19.         printData(list);  
  20.         Collections.sort(list, new Comparator<Student>() {  
  21.             @Override  
  22.             public int compare(Student o1, Student o2) {  
  23.                 if(o1.getAge() >= o2.getAge()) {  
  24.                     return 1;  
  25.                 }  
  26.                 else {  
  27.                     return -1;  
  28.                 }  
  29.             }  
  30.         });  
  31.         System.out.println("after age sorted");  
  32.         printData(list);  
  33.         Collections.sort(list, new Comparator<Student>() {  
  34.             @Override  
  35.             public int compare(Student o1, Student o2) {  
  36.                 if(o1.getAge() > o2.getAge()) {  
  37.                     return 1;  
  38.                 }  
  39.                 else if (o1.getAge() < o2.getAge()){  
  40.                     return -1;  
  41.                 }  
  42.                 else {  
  43.                     if (o1.getHeight() >= o2.getHeight()) {  
  44.                         return 1;  
  45.                     }  
  46.                     else {  
  47.                         return -1;  
  48.                     }  
  49.                 }  
  50.             }  
  51.         });  
  52.         System.out.println("after age and height sorted");  
  53.         printData(list);  
  54.     }  
  55. }  

輸出結果:

[plain] view plain copy
  1. before sorted  
  2. 學號:1 姓名:A 年齡20 身高:180  
  3. 學號:2 姓名:B 年齡21 身高:175  
  4. 學號:3 姓名:C 年齡22 身高:190  
  5. 學號:4 姓名:D 年齡21 身高:170  
  6. 學號:5 姓名:E 年齡20 身高:185  
  7. after age sorted  
  8. 學號:1 姓名:A 年齡20 身高:180  
  9. 學號:5 姓名:E 年齡20 身高:185  
  10. 學號:2 姓名:B 年齡21 身高:175  
  11. 學號:4 姓名:D 年齡21 身高:170  
  12. 學號:3 姓名:C 年齡22 身高:190  
  13. after age and height sorted  
  14. 學號:1 姓名:A 年齡20 身高:180  
  15. 學號:5 姓名:E 年齡20 身高:185  
  16. 學號:4 姓名:D 年齡21 身高:170  
  17. 學號:2 姓名:B 年齡21 身高:175  
  18. 學號:3 姓名:C 年齡22 身高:190  

單從上面的例子可以看出排序是穩定的,去看了下java的Collections.sort的源代碼,確實是基於穩定的歸併排序實現的,內部還做了優化,叫TimSort


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章