2012-11-13 8 views
1

我有一個Student對象的數組列表,其中包含多個屬性,包括名和姓,gpa,UID(大學ID號)等。我難以理解如何使用UID對數組列表進行排序。 UID是一個整數,但是,我不得不以這個項目的字符串格式。如果我可以將數字字符串解析爲int,那麼如何使用該數字從最低到最高排列列表?如何使用一個公共屬性對對象的數組列表進行排序

+0

[由屬性定義對象的排序的ArrayList]的可能的複製(http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property) – cabolanoz

回答

2
List<Student> students = // create and populate your list... 
Collections.sort(students, new Comparator<Student>() { 
    @Override 
    pulbic int compare(Student s1, Student s2) { 
     return Integer.valueOf(s1.getUid()) 
       .compareTo(Integer.valueOf(s2.getUid)); 
    } 
} 
0
Collections.sort(users, new Comparator<User>() { 
      @Override 
      public int compare(User first, User second) { 
       return Double.compare(first.getAge(), second.getAge()); 
      } 
     }); 
相關問題