0

Sort Lists in Java 8 using Comparator

This post demonstrates how to use Java 8 Comparator by examples.

Bean and Test Data

We will use the following class that represents a person with first and last names.

Our test data looks like this:

Sort by Last Name

One-level sort using Comparator.

Output:

Sort by Last Name and First Name

Two-level sort – last name and first name.

Output:

Sort by Salary

Sort by salary in descending order.

Output:

Sort by Birthdate

Sort by birthdate.

Output:

References

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert