This post demonstrates how to use Java 8 Comparator by examples.
Bean and Test Data
[wp_ad_camp_5]
We will use the following class that represents a person with first and last names.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package com.turreta.comparator; import java.time.LocalDate; public class PersonBean { private String lastName; private String firstName; private int salaryInMillions; private LocalDate birthdate; public PersonBean(String lastName, String firstName, int salaryInMillions, LocalDate birthdate) { super(); this.lastName = lastName; this.firstName = firstName; this.salaryInMillions = salaryInMillions; this.birthdate = birthdate; } public int getSalaryInMillions() { return salaryInMillions; } public String getLastName() { return lastName; } public String getFirstName() { return firstName; } public LocalDate getBirthdate() { return birthdate; } @Override public String toString() { return String.format( "PersonBean [lastName=%s, firstName=%s, salaryInMillions=%dM, birthday=%s]", lastName, firstName, salaryInMillions, birthdate); } } |
Our test data looks like this:
[wp_ad_camp_4]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | List<PersonBean> persons = new ArrayList<>(); persons.add(new PersonBean("Lamp", "Kevin", 450, LocalDate.of(1980, 1, 13))); persons.add(new PersonBean("Drine", "Michael", 230, LocalDate.of(1979, 2, 23))); persons.add(new PersonBean(null, "George", 504, LocalDate.of(1970, 12, 5))); persons.add(new PersonBean("Window", "Bill", 670, LocalDate.of(1960, 9, 16))); persons.add(new PersonBean("Black", "Buffett", 800, LocalDate.of(1985, 4, 2))); persons.add(new PersonBean("Betty", "Jeff", 506, LocalDate.of(1988, 3, 17))); persons.add(new PersonBean("Ormazo", "Amancio", 405, LocalDate.of(1970, 12, 5))); persons.add(new PersonBean("Dean", null, 506, LocalDate.of(1990, 11, 11))); persons.add(new PersonBean("Zur", "Marc", 607, LocalDate.of(1995, 3, 27))); persons.add(new PersonBean("Slim", "Shady", 506, LocalDate.of(1999, 1, 2))); persons.add(new PersonBean("Ellipse", "Larry", 770, LocalDate.of(2000, 7, 14))); persons.add(new PersonBean("Knock", "David", 980, LocalDate.of(1997, 3, 23))); persons.add(new PersonBean("Kochy", "Will", 347, LocalDate.of(2001, 1, 16))); persons.add(new PersonBean("Kochy", "Aaron", 382, LocalDate.of(1995, 3, 13))); persons.add(new PersonBean("Boomerang", "Michael", 354, LocalDate.of(1990, 8, 2))); |
Sort by Last Name
One-level sort using Comparator.
1 2 3 4 5 | persons.sort(Comparator.comparing(PersonBean::getLastName, Comparator.nullsFirst( Comparator.naturalOrder()))); persons.forEach(System.out::println); |
[wp_ad_camp_3]
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | PersonBean [lastName=null, firstName=George, salaryInMillions=504M, birthday=1970-12-05] PersonBean [lastName=Betty, firstName=Jeff, salaryInMillions=506M, birthday=1988-03-17] PersonBean [lastName=Black, firstName=Buffett, salaryInMillions=800M, birthday=1985-04-02] PersonBean [lastName=Boomerang, firstName=Michael, salaryInMillions=354M, birthday=1990-08-02] PersonBean [lastName=Dean, firstName=null, salaryInMillions=506M, birthday=1990-11-11] PersonBean [lastName=Drine, firstName=Michael, salaryInMillions=230M, birthday=1979-02-23] PersonBean [lastName=Ellipse, firstName=Larry, salaryInMillions=770M, birthday=2000-07-14] PersonBean [lastName=Knock, firstName=David, salaryInMillions=980M, birthday=1997-03-23] PersonBean [lastName=Kochy, firstName=Will, salaryInMillions=347M, birthday=2001-01-16] PersonBean [lastName=Kochy, firstName=Aaron, salaryInMillions=382M, birthday=1995-03-13] PersonBean [lastName=Lamp, firstName=Kevin, salaryInMillions=450M, birthday=1980-01-13] PersonBean [lastName=Ormazo, firstName=Amancio, salaryInMillions=405M, birthday=1970-12-05] PersonBean [lastName=Slim, firstName=Shady, salaryInMillions=506M, birthday=1999-01-02] PersonBean [lastName=Window, firstName=Bill, salaryInMillions=670M, birthday=1960-09-16] PersonBean [lastName=Zur, firstName=Marc, salaryInMillions=607M, birthday=1995-03-27] |
Sort by Last Name and First Name
Two-level sort – last name and first name.
1 2 3 4 5 6 7 8 | persons.sort( // Sort by last name Comparator.comparing(PersonBean::getLastName, Comparator.nullsFirst(Comparator.naturalOrder())) // Sort by first name .thenComparing(PersonBean::getFirstName, Comparator.nullsFirst(Comparator.naturalOrder()))); |
[wp_ad_camp_2]
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | PersonBean [lastName=null, firstName=George, salaryInMillions=504M, birthday=1970-12-05] PersonBean [lastName=Betty, firstName=Jeff, salaryInMillions=506M, birthday=1988-03-17] PersonBean [lastName=Black, firstName=Buffett, salaryInMillions=800M, birthday=1985-04-02] PersonBean [lastName=Boomerang, firstName=Michael, salaryInMillions=354M, birthday=1990-08-02] PersonBean [lastName=Dean, firstName=null, salaryInMillions=506M, birthday=1990-11-11] PersonBean [lastName=Drine, firstName=Michael, salaryInMillions=230M, birthday=1979-02-23] PersonBean [lastName=Ellipse, firstName=Larry, salaryInMillions=770M, birthday=2000-07-14] PersonBean [lastName=Knock, firstName=David, salaryInMillions=980M, birthday=1997-03-23] PersonBean [lastName=Kochy, firstName=Aaron, salaryInMillions=382M, birthday=1995-03-13] PersonBean [lastName=Kochy, firstName=Will, salaryInMillions=347M, birthday=2001-01-16] PersonBean [lastName=Lamp, firstName=Kevin, salaryInMillions=450M, birthday=1980-01-13] PersonBean [lastName=Ormazo, firstName=Amancio, salaryInMillions=405M, birthday=1970-12-05] PersonBean [lastName=Slim, firstName=Shady, salaryInMillions=506M, birthday=1999-01-02] PersonBean [lastName=Window, firstName=Bill, salaryInMillions=670M, birthday=1960-09-16] PersonBean [lastName=Zur, firstName=Marc, salaryInMillions=607M, birthday=1995-03-27] |
Sort by Salary
Sort by salary in descending order.
1 2 3 4 | persons.sort( Comparator.comparing( PersonBean::getSalaryInMillions, Comparator.reverseOrder())); |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | PersonBean [lastName=Knock, firstName=David, salaryInMillions=980M, birthday=1997-03-23] PersonBean [lastName=Black, firstName=Buffett, salaryInMillions=800M, birthday=1985-04-02] PersonBean [lastName=Ellipse, firstName=Larry, salaryInMillions=770M, birthday=2000-07-14] PersonBean [lastName=Window, firstName=Bill, salaryInMillions=670M, birthday=1960-09-16] PersonBean [lastName=Zur, firstName=Marc, salaryInMillions=607M, birthday=1995-03-27] PersonBean [lastName=Betty, firstName=Jeff, salaryInMillions=506M, birthday=1988-03-17] PersonBean [lastName=Dean, firstName=null, salaryInMillions=506M, birthday=1990-11-11] PersonBean [lastName=Slim, firstName=Shady, salaryInMillions=506M, birthday=1999-01-02] PersonBean [lastName=null, firstName=George, salaryInMillions=504M, birthday=1970-12-05] PersonBean [lastName=Lamp, firstName=Kevin, salaryInMillions=450M, birthday=1980-01-13] PersonBean [lastName=Ormazo, firstName=Amancio, salaryInMillions=405M, birthday=1970-12-05] PersonBean [lastName=Kochy, firstName=Aaron, salaryInMillions=382M, birthday=1995-03-13] PersonBean [lastName=Boomerang, firstName=Michael, salaryInMillions=354M, birthday=1990-08-02] PersonBean [lastName=Kochy, firstName=Will, salaryInMillions=347M, birthday=2001-01-16] PersonBean [lastName=Drine, firstName=Michael, salaryInMillions=230M, birthday=1979-02-23] |
Sort by Birthdate
Sort by birthdate.
[wp_ad_camp_1]
1 | persons.sort(Comparator.comparing(PersonBean::getBirthdate)); |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | PersonBean [lastName=Window, firstName=Bill, salaryInMillions=670M, birthday=1960-09-16] PersonBean [lastName=null, firstName=George, salaryInMillions=504M, birthday=1970-12-05] PersonBean [lastName=Ormazo, firstName=Amancio, salaryInMillions=405M, birthday=1970-12-05] PersonBean [lastName=Drine, firstName=Michael, salaryInMillions=230M, birthday=1979-02-23] PersonBean [lastName=Lamp, firstName=Kevin, salaryInMillions=450M, birthday=1980-01-13] PersonBean [lastName=Black, firstName=Buffett, salaryInMillions=800M, birthday=1985-04-02] PersonBean [lastName=Betty, firstName=Jeff, salaryInMillions=506M, birthday=1988-03-17] PersonBean [lastName=Boomerang, firstName=Michael, salaryInMillions=354M, birthday=1990-08-02] PersonBean [lastName=Dean, firstName=null, salaryInMillions=506M, birthday=1990-11-11] PersonBean [lastName=Kochy, firstName=Aaron, salaryInMillions=382M, birthday=1995-03-13] PersonBean [lastName=Zur, firstName=Marc, salaryInMillions=607M, birthday=1995-03-27] PersonBean [lastName=Knock, firstName=David, salaryInMillions=980M, birthday=1997-03-23] PersonBean [lastName=Slim, firstName=Shady, salaryInMillions=506M, birthday=1999-01-02] PersonBean [lastName=Ellipse, firstName=Larry, salaryInMillions=770M, birthday=2000-07-14] PersonBean [lastName=Kochy, firstName=Will, salaryInMillions=347M, birthday=2001-01-16] |
References