reverseOrder
is a static method of the Comparator
class. It is used to return a Comparator
that sorts the elements in the opposite of the natural order which is the reverse order.
The Comparator
interface is defined in the java.util
package. To import the Comparator
interface check the following import statement.
import java.util.Comparator;
public static <T extends Comparable<? super T>> Comparator<T> reverseOrder()
This method has no parameters.
The method returns a Comparator
.
import java.util.Arrays;import java.util.Comparator;public class Main{public static void main(String[] args) {// Collection of stringsString[] strings = {"hello", "educative", "edpresso"};// print the string collectionSystem.out.println("Before sorting: " + Arrays.toString(strings));// Using the naturalOrder method to sort the arrayArrays.sort(strings, Comparator.naturalOrder());// print the sorted array in natural orderSystem.out.println("After sorting in natural order: " + Arrays.toString(strings));strings = new String[]{"hello", "educative", "edpresso"};// Using the reverseOrder method to sort the array in reverseArrays.sort(strings, Comparator.reverseOrder());// print the sorted array in reverse orderSystem.out.println("After sorting in reverse order: " + Arrays.toString(strings));}}
strings
.strings
array.strings
array in the natural order.strings
array.strings
array.strings
array in the reverse order.strings
array.