How to sort by two fields in Java?

后端 未结 16 2319
离开以前
离开以前 2020-11-22 08:40

I have array of objects person (int age; String name;).

How can I sort this array alphabetically by name and then by age?

Which algorithm would

16条回答
  •  我在风中等你
    2020-11-22 08:59

    You can use Java 8 Lambda approach to achieve this. Like this:

    persons.sort(Comparator.comparing(Person::getName).thenComparing(Person::getAge));
    

提交回复
热议问题