In this post, we will learn How to sort a stream of numbers, strings and list in Java 8?”.

Sorting of a Stream in Java 8...!!! Share on X

In this tutorial, we are going to cover below topics:

  • Sorting of stream of number in Ascending(Natural) Order
  • Sorting of stream of number in Descending(Reverse) Order
  • Sorting of stream of string in Ascending Order
  • Sorting of stream of string in Descending Order
  • Sorting of stream of list in Ascending Order
  • Sorting of stream of list custom object in Ascending Order

Let’s begin:

1. Sort number in natural order

import java.util.Comparator;
import java.util.stream.Stream;

public class Number_Sort_Stream_Java8_Example1 {

    public static void main(String[] args) {

        System.out.println("Sorted stream of Numbers in Ascending Order Example");

        Stream <Integer> numberStream = Stream.of(54, 22, 78, 11, 9);

        numberStream.sorted(Comparator.naturalOrder())
            .forEach(System.out::println);
    }
}

Output:

Sorted stream of Numbers in Ascending Order Example
9
11
22
54
78

2. Sort number in reverse order

import java.util.Comparator;
import java.util.stream.Stream;

public class Number_Sort_Stream_Java8_Example2 {

    public static void main(String[] args) {

        System.out.println("Sorted stream of Numbers in Descending Order Example");

        Stream <Integer> numberStream = Stream.of(54, 22, 78, 11, 9);

        numberStream.sorted(Comparator.reverseOrder())
            .forEach(System.out::println);
    }
}

Output:

Sorted stream of Numbers in Descending Order Example
78
54
22
11
9

3. Sort string in natural order

import java.util.Comparator;
import java.util.stream.Stream;

public class String_Sort_Stream_Java8_Example1 {
    
    public static void main(String[] args) {
    
        System.out.println("Sorted stream of Strings in Ascending Order Example");

        Stream <String> stringStream = Stream.of("Tom", "Mark", "Lucas", "Amber", "Cash", "Joey");

        stringStream.sorted(Comparator.naturalOrder())
            .forEach(System.out::println);
    }
}

Output:

Sorted stream of Strings in Ascending Order Example
Amber
Cash
Joey
Lucas
Mark
Tom

4. Sort string in reverse order

import java.util.Comparator;
import java.util.stream.Stream;

public class String_Sort_Stream_Java8_Example2 {
    
    public static void main(String[] args) {
    
        System.out.println("Sorted stream of Strings in Descending Order Example");

        Stream <String> stringStream = Stream.of("Tom", "Mark", "Lucas", "Amber", "Cash", "Joey");

        stringStream.sorted(Comparator.reverseOrder())
            .forEach(System.out::println);
    }
}

Output:

Sorted stream of Strings in Descending Order Example
Tom
Mark
Lucas
Joey
Cash
Amber

5. Sort list in natural order

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

public class List_Sort_Stream_Java8_Example1 {

    public static void main(String[] args) {

        List <String> listOfStudents = Arrays.asList("Tom", "Mark", "Lucas", "Amber", "Cash", "Joey");

        System.out.println("Before Sorting:\n");
        for (String s: listOfStudents) {
            System.out.println(s);
        }

        System.out.println("\nAfter Sorting:\n");

        //Sort the list directly using Lambda expression
        listOfStudents.stream().sorted(Comparator.naturalOrder())
            .forEach(System.out::println);;

    }

}

Output:

Before Sorting:

Tom
Mark
Lucas
Amber
Cash
Joey

After Sorting:

Amber
Cash
Joey
Lucas
Mark
Tom

6. Sort custom list object

import java.util.ArrayList;
import java.util.List;

public class List_Sort_Stream_Java8_Example2 {

    public static void main(String[] args) {

        List <Actor> listOfActors = getActors();

        System.out.println("Before Sorting:\n");
        for (Actor actor: listOfActors) {
            System.out.println(actor.getAge() + "---" + actor.getName());
        }

        System.out.println("\nAfter Sorting:\n");

        //Sort the list directly using Lambda expression
        listOfActors.sort((Actor a1, Actor a2) -> a1.getAge() - a2.getAge());

        //Print the sorted list using Java 8 foreach method
        listOfActors.forEach((actor) -> System.out.println(actor.getAge() + "---" + actor.getName()));
    }

    static List <Actor> getActors() {

        List <Actor> actors = new ArrayList <Actor> ();

        actors.add(new Actor(5, "yashu"));
        actors.add(new Actor(64, "gaurangee"));
        actors.add(new Actor(22, "pulkit"));
        actors.add(new Actor(93, "isha"));
        actors.add(new Actor(8, "deepak"));
        actors.add(new Actor(44, "yogi"));
        actors.add(new Actor(87, "yashi"));

        return actors;

    }

}


class Actor {

    int age;
    String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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


    public Actor(int age, String name) {
        this.name = name;
        this.age = age;
    }


}

Output:

Before Sorting:

5---yashu
64---gaurangee
22---pulkit
93---isha
8---deepak
44---yogi
87---yashi

After Sorting:

5---yashu
8---deepak
22---pulkit
44---yogi
64---gaurangee
87---yashi
93---isha

See, how elegant and powerful Java 8 is.

Sorting of a Stream in Java 8...!!! Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts