How to sort an attribute of an object using Collections

后端 未结 6 898
眼角桃花
眼角桃花 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:08

    In Java 8 you can sort the list with a one-liner using Lambda expressions and Comparator.comparing:

    Collections.sort(studentList, Comparator.comparing(s -> s.getBirthday()));
    

    Alternatively you can use the method reference:

    Collections.sort(studentList, Comparator.comparing(Student::getBirthday));
    

提交回复
热议问题