Java Stream How to - Use Comparator.naturalOrder()








Question

We would like to know how to use Comparator.naturalOrder().

Answer

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
// sort is a default method
// naturalOrder is a static method
//w  ww  .j  a va2 s. c o  m
public class Main {
  public static void main(String... args) {

    List<Integer> numbers = Arrays.asList(3, 5, 1, 2, 6);
    numbers.sort(Comparator.naturalOrder());
    System.out.println(numbers);

  }

}

The code above generates the following result.