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
												.comparing(Employee::getFirstName)
												.thenComparing(Employee::getLastName);
		
		Collections.sort(employees, compareByName);
		
		System.out.println(employees);
	}

	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;
	}
}

//Output:

[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 !!

Reference:

Comparator.thenComparing() Java Doc

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

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

  1. Player class constructor :

    public Player (	String playerName, 
    		Integer matchesPlayed, 
    		Integer runs, 
    		Integer highestScore, 
    		Country country) 
    {
    	super();
    	this.playerName = playerName;
    	this.matchesPlayed = matchesPlayed;
    	this.runs = runs;
    	this.highestScore = highestScore;
    	this.country = 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.

    Reply
  2. 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?

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.