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?
Let us know if you liked the post. That’s the only way we can improve.
We have a data structure where some of the field values are null, so we are getting NullPointer exception, how to handle that?
You need to write custom
compare()
logic which handles thenull
values as well. Write something like NameSorter.java (Section 1.1.) in this link and check for values before comparing the field values.Or, you can see if this solution fits in your usecase.
Can we sort ignoring cases sensitivity in case of using Lambda expressions
Player class constructor :
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)
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?