1
我有一个Student对象的数组列表,其中包含多个属性,包括名和姓,gpa,UID(大学ID号)等。我难以理解如何使用UID对数组列表进行排序。 UID是一个整数,但是,我不得不以这个项目的字符串格式。如果我可以将数字字符串解析为int,那么如何使用该数字从最低到最高排列列表?如何使用一个公共属性对对象的数组列表进行排序
我有一个Student对象的数组列表,其中包含多个属性,包括名和姓,gpa,UID(大学ID号)等。我难以理解如何使用UID对数组列表进行排序。 UID是一个整数,但是,我不得不以这个项目的字符串格式。如果我可以将数字字符串解析为int,那么如何使用该数字从最低到最高排列列表?如何使用一个公共属性对对象的数组列表进行排序
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));
}
}
Collections.sort(users, new Comparator<User>() {
@Override
public int compare(User first, User second) {
return Double.compare(first.getAge(), second.getAge());
}
});
[由属性定义对象的排序的ArrayList]的可能的复制(http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property) – cabolanoz