Java stream sort a list using stream.sorted()

In this tutorial we will learn how to sort a steam. Below some example has been shown.

Java stream sort in ascending order

Example of ascending sort

package stremSort;

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

public class JavaStreamSortStringList {

    public static void main(String [] args){

        List<String> items = Arrays.asList("0", "0", "9", "C", "A", "P", "T", "A", "I", "N", "D", "R", "O", "I", "D");

        // now sort the list
        List<String> sortedItems = items
                .stream()
                .sorted()
                .collect(Collectors.toList());

        System.out.println(sortedItems);
    }
}

Result

[0, 0, 9, A, A, C, D, D, I, I, N, O, P, R, T]

Java stream sort in descending order

Example of descending sort

package stremSort;

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

public class JavaStreamSortStringListDesc {

    public static void main(String [] args){

        List<String> items = Arrays.asList("0", "0", "9", "C", "A", "P", "T", "A", "I", "N", "D", "R", "O", "I", "D");

        // now sort the list
        List<String> sortedItems = items
                .stream()
                .sorted(Comparator.reverseOrder())
                .collect(Collectors.toList());

        System.out.println(sortedItems);
    }
}

Result

[T, R, P, O, N, I, I, D, D, C, A, A, 9, 0, 0]

This is easy and straight forward, isn’t it? Below we can see the code of how to sort custom object in stream.

Java stream sort objects in ascending order

Example of ascending sort of custom object

package stremSort;

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

public class JavaStreamSortCustomObject {

    public static void main(String [] args){

        List<Toys> toyItems = Arrays.asList(
                new Toys("Toy 1", 100, 1),
                new Toys("Toy 2", 200, 2),
                new Toys("Toy 3", 300, 3),
                new Toys("Toy 4", 400, 4)
        );

        // now sort the list by price
        List<Toys> sortedItemsByPrice = toyItems
                .stream()
                .sorted(Comparator.comparingInt(Toys::getPrice))
                .collect(Collectors.toList());

        System.out.println("sorted by price\n________________");

        sortedItemsByPrice.forEach(System.out::println);

        System.out.println("\nsorted by name\n________________");

        // now sort the list by price
        List<Toys> sortedItemsByName = toyItems
                .stream()
                .sorted(Comparator.comparing(Toys::getName))
                .collect(Collectors.toList());

        sortedItemsByName.forEach(System.out::println);
    }

    static class Toys {

        private String name;
        private int price;
        private int weight;

        public Toys(String name, int price, int weight) {
            this.name = name;
            this.price = price;
            this.weight = weight;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getPrice() {
            return price;
        }

        public void setPrice(int price) {
            this.price = price;
        }

        public int getWeight() {
            return weight;
        }

        public void setWeight(int weight) {
            this.weight = weight;
        }

        @Override
        public String toString() {
            return "Toys{" +
                    "name='" + name + '\'' +
                    ", price=" + price +
                    ", weight=" + weight +
                    '}';
        }
    }
}

Result

sorted by price
________________
Toys{name='Toy 3', price=56, weight=3}
Toys{name='Toy 2', price=1234, weight=2}
Toys{name='Toy 1', price=8200, weight=1}
Toys{name='Toy 4', price=87234, weight=4}

sorted by name
________________
Toys{name='Toy 1', price=8200, weight=1}
Toys{name='Toy 2', price=1234, weight=2}
Toys{name='Toy 3', price=56, weight=3}
Toys{name='Toy 4', price=87234, weight=4}

Java stream sort objects in descending order

Example of descending sort of custom object

package stremSort;

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

public class JavaStreamSortCustomObjectDesc {

    public static void main(String [] args){

        List<Toys> toyItems = Arrays.asList(
                new Toys("Toy 1", 8200, 1),
                new Toys("Toy 2", 1234, 2),
                new Toys("Toy 3", 56, 3),
                new Toys("Toy 4", 87234, 4)
        );

        // now sort the list by price
        List<Toys> sortedItemsByPrice = toyItems
                .stream()
                .sorted(Comparator.comparingInt(Toys::getPrice).reversed())
                .collect(Collectors.toList());

        System.out.println("sorted by price in descending order\n________________________________");

        sortedItemsByPrice.forEach(System.out::println);

        System.out.println("\nsorted by name in descending order\n________________________________");

        // now sort the list by price
        List<Toys> sortedItemsByName = toyItems
                .stream()
                .sorted(Comparator.comparing(Toys::getName).reversed())
                .collect(Collectors.toList());

        sortedItemsByName.forEach(System.out::println);
    }

    static class Toys {

        private String name;
        private int price;
        private int weight;

        public Toys(String name, int price, int weight) {
            this.name = name;
            this.price = price;
            this.weight = weight;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getPrice() {
            return price;
        }

        public void setPrice(int price) {
            this.price = price;
        }

        public int getWeight() {
            return weight;
        }

        public void setWeight(int weight) {
            this.weight = weight;
        }

        @Override
        public String toString() {
            return "Toys{" +
                    "name='" + name + '\'' +
                    ", price=" + price +
                    ", weight=" + weight +
                    '}';
        }
    }
}

Result

sorted by price in descending order
________________________________
Toys{name='Toy 4', price=87234, weight=4}
Toys{name='Toy 1', price=8200, weight=1}
Toys{name='Toy 2', price=1234, weight=2}
Toys{name='Toy 3', price=56, weight=3}

sorted by name in descending order
________________________________
Toys{name='Toy 4', price=87234, weight=4}
Toys{name='Toy 3', price=56, weight=3}
Toys{name='Toy 2', price=1234, weight=2}
Toys{name='Toy 1', price=8200, weight=1}

Default image
Tushar
Articles: 59