Stream.sorted() has two overloads: a no-argument version that sorts using each element's natural ordering via Comparable.compareTo(), and a Comparator-accepting version that sorts using custom comparison logic. Internally both funnel into the same sorting machinery but differ in how elements are compared.
Sorting a List<Person> by name uses natural ordering if Person implements Comparable<Person>; sorting the same list by age instead uses people.stream().sorted(Comparator.comparingInt(Person::getAge)).
List<Person> people = getPeople();
// Natural ordering (Person implements Comparable<Person>)
people.stream().sorted().forEach(System.out::println);
// Custom comparator
people.stream()
.sorted(Comparator.comparingInt(Person::getAge))
.forEach(System.out::println);A concise interview answer is
Natural ordering relies on the element implementing Comparable and calls compareTo() internally, while the Comparator overload lets me plug in arbitrary comparison logic without changing the class. Both are stateful operations — the stream has to buffer all elements before it can sort, using a stable, Tim Sort-based algorithm under the hood, which is why sorted() doesn't work well on infinite streams without a preceding limit().
Last updated: 16 July 2026
Previous question: What is the difference between Optional.of() and Optional.ofNullable()?
Next question: Can you use Optional as a method parameter? Why should or shouldn’t you do this?