I have an arraylist of Student objects which have several properties including first and last name, gpa, UID (university ID number), and more. I am stumped on how to sort the arraylist using the UID. The UID is an integer number, however, I'm forced to have it in String format for this project. If I can parse the numeric string into an int, how then can I sort the arraylist from lowest to highest using that number?
Asked
Active
Viewed 1,033 times
1
-
Possible duplicate of [Sort ArrayList of custom Objects by property](http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property) – cabolanoz Apr 29 '17 at 05:04
2 Answers
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));
}
}

Costi Ciudatu
- 37,042
- 7
- 56
- 92
0
Collections.sort(users, new Comparator<User>() {
@Override
public int compare(User first, User second) {
return Double.compare(first.getAge(), second.getAge());
}
});

chry
- 434
- 7
- 5