java對象排序(Collections.sort Comparator)


       我們在一般的使用過程中對於數組、集合等的排序,如果不涉及對象的話就比較容易。一般都有現成的API方法可以直接使用。

當要對對象集合排序時候,現在基本都重寫Comparator類的compare方法來實現。

public static <T> void sort(List<T> list, Comparator<? super T> c)

創建一個Student類,包含姓名、性別、身高、體重。當有許多Student時候,此時就需要一個集合包含這些對象。我們要通過身高來對這些Student排序,就要重寫Comparator了。

話不多少,上代碼。

創建Strudent類,生成getter、setter方法和構造方法。

 1 public class Student {  2     private String name;  3     private String sex;  4     private Double height;  5     private Double weight;  6     public Student(String name, String sex, Double height, Double weight) {  7         this.name = name;  8         this.sex = sex;  9         this.height = height; 10         this.weight = weight; 11  } 12     public String getName() { 13         return name; 14  } 15 
16     public void setName(String name) { 17         this.name = name; 18  } 19 
20     public String getSex() { 21         return sex; 22  } 23 
24     public void setSex(String sex) { 25         this.sex = sex; 26  } 27 
28     public Double getHeight() { 29         return height; 30  } 31 
32     public void setHeight(Double height) { 33         this.height = height; 34  } 35 
36     public Double getWeight() { 37         return weight; 38  } 39 
40     public void setWeight(Double weight) { 41         this.weight = weight; 42  } 43 
44 }

創建List<Student>,並進行排序

 1  public static void main(String[] args) throws Exception{  2         Student xiaoMing = new Student("Xiao Ming", "Male", 175.25d, 65.5d);  3         Student xiaoHong = new Student("Xiao Hong", "Female", 165.1d, 50.12d);  4         Student xiaoLi = new Student("Xiao Li", "Female", 180.5d, 70.12d);  5         List<Student> list =  new ArrayList<>();  6  list.add(xiaoMing);  7  list.add(xiaoLi);  8  list.add(xiaoHong);  9         for(Student s : list) { 10  System.out.println(s.getName()); 11  } 12         System.out.println("=================開始排序=============="); 13         Collections.sort(list, new Comparator<Student>() { 14  @Override 15             //關於返回值,此處有高人說明:返回1表示true,需要調整順序,返回-1表示false不需要排序,不必糾結升序還是降序,你只要關系你需要不需要調整順序即可
16             public int compare(Student s1, Student s2) { 17                 if (s1.getHeight().compareTo(s2.getHeight()) > 0) { 18                     return -1; 19                 } else { 20                     return 1; 21  } 22  } 23  }); 24         //輸出排序結果
25         for(Student s : list) { 26  System.out.println(s.getName()); 27  } 28 
29     }

輸出結果

Xiao Ming Xiao Li Xiao Hong =================開始排序============== Xiao Li Xiao Ming Xiao Hong

調整返回值結果

 1 Collections.sort(list, new Comparator<Student>() {  2  @Override  3             //關於返回值,此處有高人說明:返回1表示true,需要調整順序,返回-1表示false不需要排序,不必糾結升序還是降序,你只要關系你需要不需要調整順序即可
 4             public int compare(Student s1, Student s2) {  5                 if (s1.getHeight().compareTo(s2.getHeight()) > 0) {  6                     return 1;  7                 } else {  8                     return -1;  9  } 10  } 11         });

再次運行查看結果

Xiao Ming Xiao Li Xiao Hong =================開始排序============== Xiao Hong Xiao Ming Xiao Li

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM