How to sort an arraylist of objects using one common property

后端 未结 2 1354
無奈伤痛
無奈伤痛 2020-12-22 01:29

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 th

相关标签:
2条回答
  • 2020-12-22 02:14
    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 讨论(0)
  • 2020-12-22 02:20
     Collections.sort(users, new Comparator<User>() {
                @Override
                public int compare(User first, User second) {
                    return Double.compare(first.getAge(), second.getAge());
                }
            });
    
    0 讨论(0)
提交回复
热议问题