JavaIntermediate#collections

How do you sort a List of custom objects by multiple fields?

Use Comparator.comparing() chained with thenComparing() to define a primary sort key followed by tie-breaking keys, applied via List.sort() or Collections.sort().

Example
list.sort(Comparator.comparing(Person::getLastName)
  .thenComparing(Person::getFirstName));

Related Questions

1
JavaBeginner#generics

What is the diamond operator in generics?

Open
2
JavaBeginner#exceptions

What is the exception hierarchy in Java?

Open
3
JavaBeginner#exceptions

What is a custom exception and how do you create one?

Open