How to sort an attribute of an object using Collections

后端 未结 6 893
眼角桃花
眼角桃花 2020-12-03 11:52

Good day!

I have an object student with the following attributes:

class Student
    String name
    Date birthday

I used arrayList

6条回答
  •  孤城傲影
    2020-12-03 12:26

    You need to write a custom comparator.

    Something like:

    Collections.sort(studentList, new Comparator() {
    
        public int compare(Strudent a, Strudent b) {
            return a.birthday.compareTo(b.birthday);
        }
    
    });
    

提交回复
热议问题