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

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Sep 11, 2019, updated Feb 18, 2020. Topics: Java.
Canonical URL: https://attacomsian.com/blog/sort-list-using-stream-sorted-method-java

> Learn how to sort a list using Stream sorted() method using natural ordering as well as ordering provided by Comparator 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:

```java
// 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:

```plaintext
CA
CN
DE
FR
PK
US
```

### Reverse Order

We can also reverse the natural ordering by providing a `Comparator` to the `sorted()` method:

```java
// 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:

```plaintext
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: 

```java
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:

```java
// 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:

```plaintext
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:

```java
// 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:

```plaintext
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](https://attacomsian.com/blog/how-to-sort-a-map-in-java)
