Site Search:

Sort a collection using Stream API

Back>

stream.sorted()
stream.sorted()

Stream have two methods for sort a stream:

  • Stream<T> sorted();
  • Stream<T> sorted(Comparator<? super T> comparator);
These two methods remind us of the two Collections static sort methods:
  • public static <T extends Comparable<? super T>> void sort(List<T> t) {list.sort(null);}
  • public static <T extends Comparable<? super T>> void sort(List<T> t, Comparator<? super T> c) {list.sort(c);}


Stream.sorted is an intermediate operation. As any stream operations, it didn't change the original stream source.

OCPJP>cat SortDemo.java 
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

public class SortDemo {
    public static void main(String[] args) {
        List<String> list = new LinkedList<>();
        list.add("a");
        list.add("BB");
        list.add("AAAA");
        list.add("bbb");
        Collections.sort(list);
        System.out.println("Collections.sort with natural order: " + list);
        
        Comparator<String> comparator = Comparator.comparingInt(String::length);
        Collections.sort(list, comparator);
        System.out.println("Collections.sort with comparator: " + list);
        
        Comparator<String> comparator2 = (i, j) -> i.length() - j.length();
        Collections.sort(list, comparator2);
        System.out.println("Collections.sort with equivalent comparator2: " + list);
        
        Comparator<String> comparator3 = Comparator.naturalOrder();
        Collections.sort(list, comparator3);
        System.out.println("The default sort can also be expressed as Comparator.naturalOrder: " + list);
        
        Collections.shuffle(list);
        System.out.println("shuffled collection: " + list);
        
        List<String> list2 = list.stream().sorted().collect(Collectors.toList());
        System.out.println("Stream.sorted with natural order: " + list2);
        System.out.println("stream didn't change the original collection: " + list);
        list2 = list.stream().sorted(comparator).collect(Collectors.toList());
        System.out.println("stream sorted with comparator: " + list2);
        System.out.println("stream didn't change the original collection: " + list);
        list2 = list.stream().sorted(comparator2).collect(Collectors.toList());
        System.out.println("stream sorted with equivalent comparator2: " + list2);
        list2 = list.stream().sorted(comparator3).collect(Collectors.toList());
        System.out.println("The default sorted can also be expressed as Comparator.naturalOrder: " + list2);
    }
}
OCPJP>
OCPJP>
OCPJP>
OCPJP>javac SortDemo.java 
OCPJP>java SortDemo
Collections.sort with natural order: [AAAA, BB, a, bbb]
Collections.sort with comparator: [a, BB, bbb, AAAA]
Collections.sort with equivalent comparator2: [a, BB, bbb, AAAA]
The default sort can also be expressed as Comparator.naturalOrder: [AAAA, BB, a, bbb]
shuffled collection: [a, bbb, BB, AAAA]
Stream.sorted with natural order: [AAAA, BB, a, bbb]
stream didn't change the original collection: [a, bbb, BB, AAAA]
stream sorted with comparator: [a, BB, bbb, AAAA]
stream didn't change the original collection: [a, bbb, BB, AAAA]
stream sorted with equivalent comparator2: [a, BB, bbb, AAAA]
The default sorted can also be expressed as Comparator.naturalOrder: [AAAA, BB, a, bbb]
OCPJP>


We are not done, yet, there are primitive streams to visit.
Fortunately, these are simple, they don't have comparator version:

        DoubleStream.of(0.1, .2, .5, .3).sorted();
        IntStream.of(1, 2, 5, 4).sorted();

        LongStream.of(1, 2L, 5, 3).sorted();

One of the shortest topic for Stream.