Comparator reversed() method in Java with examples
The reversed() method of Comparator Interface in Java returns a comparator that imposes the reverse ordering of this comparator. If you use sort method of the array and passes this comparator after applying the reversed method then it will sort the array in reverse order.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
default Comparator<T> reversed()
Parameters: This method accepts nothing.
Return value: This method returns a comparator that imposes the reverse ordering of this comparator.
Below programs illustrate reversed() method:
before sort : [aman, amar, avik] after sort : [avik, amar, aman]
Before sorting: [KKR, CSK, MI, KXIP, RCB, SRH, DC, RR] After sorting: [SRH, RR, RCB, MI, KXIP, KKR, DC, CSK]