How to sort a List using Stream sorted() in Java

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.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.