Java 8 Stream – How to sort a List with stream.sorted()
In this section, we will show you how to sort list using stream.sorted() in Java 8. 1. List 1.1 Sort a List in natural order. import java.util.Arrays ; import java.util.Comparator ; import java.util.List ; import java.util.stream.Collectors ; public class Main { public static void main ( String [] args) { List < String > list = Arrays . asList ( "8" , "B" , "A" , "1" , "Z" , "Y" , "2" , "b" , "d" ); List < String > sortedList1 = list .stream() .sorted( Comparator . naturalOrder ()) .collect( Collectors . toList ()); System . out .println( sortedList1 ); List < String > sortedList2 = list .stream() .sorted((o1,o2)-> o1.compareTo(o2)) .collect( Collectors . toList ()); System . out .println( sortedList2 ); List < String > sortedList3 = list .stream(). sorted().