Apparently, it's not documented or I missed it.
Here's the link to the documentation and below's the text as an image:
EDIT(17/5): I think too many confused this question to be a comparator question. It is not. The comparator compares between 2 elements. According to that comparison, the list sorted. How? Ascending or Descending?
I'll refine/simplify the question even further: If the comparator decides that element A is smaller than element B. In the sorted list, will element A be located at a lower index than element B?
The default sorting order for an object is ascending order like Integer will be sorted from low to high while descending order is just opposite. Collections. reverseOrder() returns a Comparator which will be used for sorting Objects in descending order.
sort() method is present in java. util. Collections class. It is used to sort the elements present in the specified list of Collection in ascending order.
By default, Collection. sort performs the sorting in ascending order. If we want to sort the elements in reverse order we could use following methods: reverseOrder() : Returns a Comparator that imposes the reverse of natural ordering of elements of the collection.
Collections sort is a method of Java Collections class used to sort a list, which implements the List interface. All the elements in the list must be mutually comparable. If a list consists of string elements, then it will be sorted in alphabetical order.
java.util.Collections.sort() method is present in java.util.Collections class. It is used to sort the elements present in the specified list of Collection in ascending order. It works similar to java.util.Arrays.sort() method but it is better then as it can sort the elements of Array as well as linked list, queue and many more present in it.
The java.Collections.sort () is very similar to the java.util.Arrays.sort () method; rather it is much better than it. The java.Collections.sort () method is also used to sort the linked list, array, queue, and other data structures.
The sort () method of List Interface sorts the given list according to the order specified in the comparator. The list must be modifiable else it will throw an exception. The parameter 'c' represents the Comparator used to compare list elements.
Checkout for comparison logic in compareTo () method. Nest Java program sorts the list of Employee objects by their name; 2. Custom Sorting using Comparators The second parameter in sort () method takes an instance of Comparator.
The sort order is always ascending, where the Comparator defines which items are larger than others.
From the documentation for Collections.sort(List<T> list, Comparator<? super T> c):
Sorts the specified list according to the order induced by the specified comparator.
From the documentation for Comparator.compare(T,T):
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With