Description
The assumed arrayListName actually a variable of type ArrayList , then you call the List#sort method here. From the documentation :
default void sort(Comparator<? super E> c)
Sorts this list according to the order invoked by the specified Comparator .
If the specified comparator is null , then all elements in this list should implement the Comparable interface, and the natural order should be used ... p>
Thus, the method uses the natural order of elements when the comparator is null .
This natural ordering is set by the compareTo method for elements when they implement the Compareable interface ( documentation ). For int this view is incremented. For String this type is based on lexicographic order .
Examples after sorting with natural ordering:
1, 2, 3, 3, 3, 8, 11 "A", "B", "H", "Helicopter", "Hello", "Tree"
Many classes already implement this interface. Take a look at the documentation . Currently considered 287 classes.
More details
Compare this with the actual implementation :
@Override @SuppressWarnings("unchecked") public void sort(Comparator<? super E> c) { final int expectedModCount = modCount; Arrays.sort((E[]) elementData, 0, size, c); if (modCount != expectedModCount) { throw new ConcurrentModificationException(); } modCount++; }
The comparator c is passed to the Arrays#sort method, let's look at an excerpt from the implementation :
if (c == null) { sort(a, fromIndex, toIndex); }
We follow this call to another Arrays#sort method ( implementation ). This method sorts items based on their natural ordering . Thus, no comparator is used.