In this brief article, you will learn how to sort a list using Stream's sorted()
method in Java. We can sort the stream elements in natural ordering as well as the ordering provider by Comparator
.
Sort List in Natural Ordering
Here is an example that sort the list of strings in natural ordering and print the elements:
// create a list
List<String> list = Arrays.asList("US", "FR", "DE", "CN", "PK", "CA");
// sort the list in natural ordering
List<String> sorted = list.stream().sorted().collect(Collectors.toList());
// print the elements
sorted.forEach(System.out::println);
Here is the output that shows the list is sorted in natural ordering:
CA
CN
DE
FR
PK
US
Reverse Order
We can also reverse the natural ordering by providing a Comparator
to the sorted()
method:
// create a list
List<String> list = Arrays.asList("US", "FR", "DE", "CN", "PK", "CA");
// sort the list in natural ordering
List<String> sorted = list.stream()
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
// print the elements
sorted.forEach(System.out::println);
Now the output looks like below:
US
PK
FR
DE
CN
CA
Sort List of Objects
The sorted()
method can be used to sort a list of objects as well. Let us create a simple User
class that we will use to sort by using streams:
package com.attacomsian;
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
// getters and setters removed for brevity
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
Here is an example that sorts the list of users by age
using natural ordering:
// create a list
List<User> list = new ArrayList<>();
list.add(new User("Jovan", 33));
list.add(new User("Michael", 45));
list.add(new User("Atta", 29));
list.add(new User("Emma", 20));
// sort the list in natural ordering
List<User> sorted = list.stream()
.sorted(Comparator.comparingInt(User::getAge))
.collect(Collectors.toList());
// print users
sorted.forEach(System.out::println);
Here the output:
User{name='Emma', age=20}
User{name='Atta', age=29}
User{name='Jovan', age=33}
User{name='Michael', age=45}
Reverse Order
If you want to sort the list of objects in reverse order, use the reversed()
method of Comparator
. Here is an example that sorts the above list by user's age in reverse order:
// create a list
List<User> list = new ArrayList<>();
list.add(new User("Jovan", 33));
list.add(new User("Michael", 45));
list.add(new User("Atta", 29));
list.add(new User("Emma", 20));
// sort the list in natural ordering
List<User> sorted = list.stream()
.sorted(Comparator.comparingInt(User::getAge).reversed())
.collect(Collectors.toList());
// print users
sorted.forEach(System.out::println);
The output in reverse order:
User{name='Michael', age=45}
User{name='Jovan', age=33}
User{name='Atta', age=29}
User{name='Emma', age=20}
Read Next: How to sort a Map using streams in Java
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.