Stream sorted (Comparator comparator) method in Java

Stream sorted(Comparator comparator) returns a stream consisting of the elements of this stream, sorted according to the provided Comparator. For ordered streams, the sort method is stable but for unordered streams, no stability is guaranteed. It is a stateful intermediate operation i.e, it may incorporate state from previously seen elements when processing new elements. In java 8, Comparator can be instantiated using lambda expression. We can also reverse the natural ordering as well as ordering provided by Comparator.
Syntax :

Stream<T> sorted(Comparator<? super T> comparator)

Where, Stream is an interface and T
is the type of stream elements.
comparator is used to compare stream elements.

Below given are some examples to understand the implementation of the function in a better way.

Example 1 :



filter_none

edit
close

play_arrow

link
brightness_4
code

// Implementation of Stream.sorted()
// to get a stream of sorted elements
// according to the provided Comparator
import java.util.*;
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating a list of Integers
        List<Integer> list = Arrays.asList(5, -10, 7, -18, 23);
  
        System.out.println("The sorted stream according "
                           + "to provided Comparator is : ");
  
        // Displaying the list of Strings in
        // reverse order after sorting
        list.stream().sorted(Comparator.reverseOrder()).
                          forEach(System.out::println);
    }
}

chevron_right


Output :

The sorted stream according to provided Comparator is : 
23
7
5
-10
-18

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Implementation of Stream.sorted()
// to get a stream of sorted elements
// according to the provided Comparator
import java.util.*;
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating a list of Strings
        List<String> list = Arrays.asList("Geeks", "for",
                    "GeeksforGeeks", "GeeksQuiz", "GFG");
  
        System.out.println("The sorted stream according "
                           + "to provided Comparator is : ");
  
        // Displaying the list of Strings in
        // reverse order after sorting
        list.stream().sorted(Comparator.reverseOrder()).
                            forEach(System.out::println);
    }
}

chevron_right


Output :

The sorted stream according to provided Comparator is : 
for
GeeksforGeeks
GeeksQuiz
Geeks
GFG

Example 3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Implementation of Stream.sorted()
// to get a stream of sorted elements
import java.util.*;
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating an array of Strings
        String[] array = { "GFG", "Geeks", "for",
                           "GeeksforGeeks", "GeeksQuiz" };
  
        System.out.println("The sorted stream is :");
  
        // sorting the elements of array based
        // on their last character
        Stream.of(array).sorted((str1, str2)
                     -> Character.compare(str1
                     .charAt(str1.length() - 1),
                    str2.charAt(str2.length() - 1)))
            .         forEach(System.out::println);
    }
}

chevron_right


Output :

The sorted stream is :
GFG
for
Geeks
GeeksforGeeks
GeeksQuiz


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.