Java 8 – Comparator thenComparing() example

Java example to sort list of objects by multiple fields using Comparator.thenComparing() method. This method returns a lexicographic-order comparator with another comparator. It gives the same effect as SQL group by clause.

Quick Reference:

//first name comparator
Comparator<Employee> compareByFirstName = Comparator.comparing( Employee::getFirstName );

//last name comparator
Comparator<Employee> compareByLastName = Comparator.comparing( Employee::getLastName );

//Compare by first name and then last name (multiple fields)
Comparator<Employee> compareByFullName = compareByFirstName.thenComparing(compareByLastName);

//Use Comparator
Collections.sort(employees, compareByFullName);

Sort on multiple fields – Group by sort

Example of using thenComparing() to create Comparator which is capable of sorting by multiple fields.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class JavaSort 
	public static void main(String[] args) 
		ArrayList<Employee> employees = getUnsortedEmployeeList();
		//Compare by first name and then last name
		Comparator<Employee> compareByName = Comparator
		Collections.sort(employees, compareByName);

	private static ArrayList<Employee> getUnsortedEmployeeList() 
		ArrayList<Employee> list = new ArrayList<>();
		list.add( new Employee(2, "Lokesh", "Gupta") );
		list.add( new Employee(1, "Alex", "Gussin") );
		list.add( new Employee(4, "Brian", "Sux") );
		list.add( new Employee(5, "Neon", "Piper") );
		list.add( new Employee(3, "David", "Beckham") );
		list.add( new Employee(7, "Alex", "Beckham") );
		list.add( new Employee(6, "Brian", "Suxena") );
        return list;


[E[id=7, firstName=Alex, 	lastName=Beckham], 
E [id=1, firstName=Alex, 	lastName=Gussin], 
E [id=4, firstName=Brian, 	lastName=Sux], 
E [id=6, firstName=Brian, 	lastName=Suxena], 
E [id=3, firstName=David, 	lastName=Beckham], 
E [id=2, firstName=Lokesh, 	lastName=Gupta], 
E [id=5, firstName=Neon, 	lastName=Piper]]

Drop me your questions in comments section.

Happy Learning !!


Comparator.thenComparing() Java Doc

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

6 thoughts on “Java 8 – Comparator thenComparing() example”

  1. We have a data structure where some of the field values are null, so we are getting NullPointer exception, how to handle that?

    Exception in thread "main" java.lang.NullPointerException
    	at java.base/java.util.Comparator.lambda$comparing$77a9974f$1(
    	at java.base/java.util.Comparator.lambda$thenComparing$36697e65$1(
    	at java.base/java.util.Comparator.lambda$thenComparing$36697e65$1(
    	at java.base/java.util.Comparator.lambda$thenComparing$36697e65$1(
    	at java.base/java.util.TimSort.binarySort(
    	at java.base/java.util.TimSort.sort(
    	at java.base/java.util.Arrays.sort(
    	at java.base/java.util.ArrayList.sort(
    	at java.base/java.util.Collections.sort(
  2. Player class constructor :

    public Player (	String playerName, 
    		Integer matchesPlayed, 
    		Integer runs, 
    		Integer highestScore, 
    		Country country) 
    	this.playerName = playerName;
    	this.matchesPlayed = matchesPlayed;
    	this.runs = runs;
    	this.highestScore = highestScore; = country;
    public static List players = Arrays.asList(
    	new Player("Virat", 52, 5200, 100, new Country(1, "India")),
    	new Player("Williamson", 4200, 550, 90, new Country(2, "New Zealand")),
    	new Player("Smith", 262, 6500, 35, new Country(3, "Australia")),
    	new Player("Chris", 321, 7000, 150, new Country(4, "West Indies")),
    	new Player("Gayle", 250, 3200, 45, new Country(4, "West Indies")),
    	new Player("MS Dhoni", 102, 5020, 109, new Country(5, "India"))

    Suppose I have a above list. Player and country has a relationship.

    How can I sort the list by country name and then by matches Played in reversed order.

    • Suppose I have.

      public Country (Integer id, String countryName)

      Comparator compareByCountryAndPlayer = Comparator
              .comparing(p -> p.getCountry().getCountryName())
              .thenComparing(Player::getMatchesPlayed, Comparator.reverseOrder());
  3. Nice tutorial but I am stuck with another problem.How to sort a list of object based on two field where we have to sort one field in ascending order and one field in descending order?


Leave a Comment


A blog about Java and related technologies, the best practices, algorithms, and interview questions.