Skip to content
Sharp

Collections · interview question

Which interface should a class implement to define its natural ordering, so that Collections.sort(list) works without a comparator?

Fill in the blankEasy
Answer and explanation

Correct answer

Comparable or Comparable<T> or java.lang.Comparable

Why

Comparable<T> has one method, compareTo. For orderings other than the natural one, pass a Comparator, e.g. Comparator.comparing(Person::age).

Test yourself on Collections

A free test with instant, explained results. No account needed.

Start free test
All Collections questions →