Name two methods that allow sorting a list of objects, and the prerequisites for their use.
Exercise – Sort list of objects
Answer
Two static methods of the class java.util.Collections:
- void sort(List<T> list): Sorts a list of objects that implement the interface Comparable (the compareTo(T) method),
- void sort(List<T> list, Comparator<T> comparator): Sorts objects according to the Comparator provided (the compare(T o1, T o2) method)