1

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
MobileCrysis
  • 31
  • 1
  • 3
  • 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 Answers2

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