Software Development

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.



Sort by Last Name and First Name

Two-level sort – last name and first name.



Sort by Salary

Sort by salary in descending order.


Sort by Birthdate

Sort by birthdate.




Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.

You Might Also Like