How to sort an attribute of an object using Collections

You can pass a Comparator to Collections.sort() to handle the sorting by birthday:

Collections.sort(studentList, new Comparator<Student>() {
    public int compare(Student s1, Student s2) {
        return s1.getBirthday().compareTo(s2.getBirthday());
    }
});

You’ll need to add getBirthday() to your Student class if you don’t have it already.

Leave a Comment