Sort objects on multiple fields /properties – Comparator interface (lambda stream java 8)

  • Given a list of user defined objects, we would like sort the objects on multiple field or properties.
  • We will use java 8 lambda stream to sort objects.
  • We will create POJO of Person object. We will sort the list of person objects by their firstName.
  • We will demonstrate the cascade sorting.
    • We will sort the list of objects on multiple fields.

1. Sort Objects on multiple properties – Comparator interface (lambda java8)

We will perform following operations on user defined objects or POJO

  1. Create a Person Class
  2. Create a arraylist of Person objects.
  3. We will demonstrate following sorting operations:
    1. Sorted the list of person objects by firstName
    2. Cascade Sort arraylist of person objects by
      • firstName then
      • lastName then
      • age
    3. We will write three Comparators to demonstrate cascade sorting
      • Comparator to sort objects by first name.
      • Comparator to sort objects by last name.
      • Comparator to sort objects by age.

2. Sort user defined object on multiple fields – Comparator (lambda stream)

package org.learn;
 
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
 
public class SortObjectByField {
 
    public static void main(String[] args) {
        List <Person> personList = new ArrayList<>();
        personList.add(new Person("Mike", "harvey", 34, "001894536"));
        personList.add(new Person("Nick", "young", 75"005425676"));
        personList.add(new Person("Jack", "slater", 21 ,"009654153"));
        personList.add(new Person("gary", "hudson", 55,"00564536"));
        personList.add(new Person("Mike", "harvey", 21 ,"003685417"));
        personList.add(new Person("gary", "hudson", 25,"00452341"));
         
        System.out.println("1. Sort List of person objects by firstName");
        //Sort by First Name
        personList
            .stream()
            .sorted(
                    (person1,person2) ->
                    person1.firstName.
                        compareToIgnoreCase(person2.firstName) 
                   )
            .forEach(
                        person->
                        System.out.println(person)
                    );
         
        //Sort by first and last name
        System.out.println("\n2.Sort list of person objects by firstName then "
                                            + "by lastName then by age");
        Comparator<Person> sortByFirstName
                                = (p, o) -> p.firstName.compareToIgnoreCase(o.firstName);
        Comparator<Person> sortByLastName
                                = (p, o) -> p.lastName.compareToIgnoreCase(o.lastName);
        Comparator<Person> sortByAge
                                = (p, o) -> Integer.compare(p.age,o.age);
         
        //Sort by first Name then Sort by last name then sort by age
        personList
        .stream()
        .sorted(
                sortByFirstName
                    .thenComparing(sortByLastName)
                    .thenComparing(sortByAge)
               )
        .forEach(
                person->
                    System.out.println(person)
                );     
    }
}

3. O/P: Sort objects on multiple fields – Comparator interface (lambda stream)

1. Sort List of person objects by firstName
[gary hudson 55 00564536]
[gary hudson 25 00452341]
[Jack slater 21 009654153]
[Mike harvey 34 001894536]
[Mike harvey 21 003685417]
[Nick young 75 005425676]
 
2.Sort list of person objects by firstName then by lastName then by age
[gary hudson 25 00452341]
[gary hudson 55 00564536]
[Jack slater 21 009654153]
[Mike harvey 21 003685417]
[Mike harvey 34 001894536]
[Nick young 75 005425676]