The Java Collections reverseOrder() method is used to get a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.
DeclarationFollowing is the declaration for java.util.Collections.reverseOrder() method.
public static <T> Comparator<T> reverseOrder()Parameters
NA
Return ValueThe method call returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.
ExceptionNA
Java Collections reverseOrder(Comparator<T>) Method DescriptionThe reverseOrder(Comparator<T>) method is used to get a comparator that imposes the reverse ordering of the specified comparator.
DeclarationFollowing is the declaration for java.util.Collections.reverseOrder() method.
public static <T> Comparator<T> reverseOrder(Comparator<T> cmp)Parameters
cmp − This is the comparator.
Return ValueThe method call returns a comparator that imposes the reverse ordering of the specified comparator.
ExceptionNA
Getting Reverse Order Comparator for a List of Integers ExampleThe following example shows the usage of Java Collection reverseOrder() method. We've created a List object with some integers, printed the original list. Using reverseOrder() method, we've retrieved a comparator and then reversed sorted the list and printed it.
package com.tutorialspoint; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; import java.util.List; public class CollectionsDemo { public static void main(String args[]) { // create a list object List<Integer> list = new LinkedList<>(Arrays.asList(-28,20,-12,8)); System.out.println(list); // create comparator for reverse order Comparator<Integer> cmp = Collections.reverseOrder(); // sort the list Collections.sort(list, cmp); System.out.println("List sorted in ReverseOrder: "); System.out.println(list); } }Output
Let us compile and run the above program, this will produce the following result.
[-28, 20, -12, 8] List sorted in ReverseOrder: [20, 8, -12, -28]Getting Reverse Order Comparator for a List of Strings Example
The following example shows the usage of Java Collection reverseOrder() method. We've created a List object with some strings, printed the original list. Using reverseOrder() method, we've retrieved a comparator and then reversed sorted the list and printed it.
package com.tutorialspoint; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; import java.util.List; public class CollectionsDemo { public static void main(String args[]) { // create a list object List<String> list = new LinkedList<>(Arrays.asList("A","B","C","D")); System.out.println(list); // create comparator for reverse order Comparator<String> cmp = Collections.reverseOrder(); // sort the list Collections.sort(list, cmp); System.out.println("List sorted in ReverseOrder: "); System.out.println(list); } }Output
Let us compile and run the above program, this will produce the following result.
[A, B, C, D] List sorted in ReverseOrder: [D, C, B, A]Getting Reverse Order Comparator for a List of Objects Example
The following example shows the usage of Java Collection reverseOrder(Comparator) method. We've created a List object with some Student objects, printed the original list. Using reverseOrder(Comparator) method, we've retrieved a comparator and then reversed sorted the list and printed it.
package com.tutorialspoint; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; import java.util.List; public class CollectionsDemo { public static void main(String args[]) { // create a list object List<Student> list = new LinkedList<>(Arrays.asList(new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert"))); System.out.println(list); RollNoComparator comparator = new RollNoComparator(); // create comparator for reverse order Comparator<Student> cmp = Collections.reverseOrder(comparator); // sort the list Collections.sort(list, cmp); System.out.println("List sorted in ReverseOrder: "); System.out.println(list); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class RollNoComparator implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { return o1.getRollNo()-o2.getRollNo(); } }Output
Let us compile and run the above program, this will produce the following result.
[[ 1, Julie ], [ 3, Adam ], [ 2, Robert ]] List sorted in ReverseOrder: [[ 3, Adam ], [ 2, Robert ], [ 1, Julie ]]
java_util_collections.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4