How to sort an arraylist of objects using one common property

后端 未结 2 1353
無奈伤痛
無奈伤痛 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 students = // create and populate your list...
    Collections.sort(students, new Comparator() {
        @Override
        pulbic int compare(Student s1, Student s2) {
            return Integer.valueOf(s1.getUid())
                    .compareTo(Integer.valueOf(s2.getUid));
        }
    }
    

提交回复
热议问题