September 11, 2021

Java Stream Sort on Multiple Fields

In this tutorial you’ll see how to sort stream of objects on multiple fields.

Sort stream of objects on multiple fields

To sort a stream of objects on multiple fields you need to use two methods-

1. Stream<T> sorted(Comparator<? super T> comparator)- sorts the elements of this stream according to the provided Comparator.

2. Since sorting is to be done on multiple fields for that you can compose several Comparators using thenComparing(Comparator<? super T> other) method.

Comparator.comparing(COMPARISON_LOGIC)
          .thenComparing(COMPARISON_LOGIC);

Java Stream sorting with multiple fields example

For the example we’ll use the object of User class which has two fields name and age.

public class User {
  private String name;
  private int age;
  User(String name, int age){
    this.name = name;
    this.age = age;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  @Override
  public String toString() {
    return getName() + " " + getAge() + " \n";
  } 
}

If we want to sort on name as well as on age in descending order then it can be done using sorted method of the Java Stream API as given below.

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class StreamSort {

  public static void main(String[] args) {
      List<User> userList = Arrays.asList(new User("Peter", 75),
              new User("Ram", 19),
              new User("Peter", 68),
              new User("Mahesh", 32),
              new User("Scott", 32));
      userList = userList.stream()
                         .sorted(Comparator.comparing(User::getName)
                                  .thenComparing(Comparator.comparingInt(User::getAge).reversed()))
                         .collect(Collectors.toList());
      System.out.println(userList);

  }
}
Output
[Mahesh 32 
, Peter 75 
, Peter 68 
, Ram 19 
, Scott 32 
]

To make it clearer here is the elongated version with pre-defined Comparators.

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class StreamSort {

  public static void main(String[] args) {
      List<User> userList = Arrays.asList(new User("Peter", 75),
              new User("Ram", 19),
              new User("Peter", 68),
              new User("Mahesh", 32),
              new User("Scott", 32));
      
      Comparator<User> compByName = Comparator.comparing(User::getName);
      Comparator<User> compByAge = Comparator.comparingInt(User::getAge).reversed();
      userList = userList.stream()
                         .sorted(compByName
                               .thenComparing(compByAge))
                         .collect(Collectors.toList());
      System.out.println(userList);
  }
}

That's all for the topic Java Stream Sort on Multiple Fields. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment