Introduction
- The reverseOrder() method of Collections class that in itself is present inside
java.util package
- returns a comparator
- using this comparator we can order the Collection in reverse order.
- Natural ordering is the ordering imposed by the objects’ own compareTo method.
Syntax:
public static Comparator reverseOrder()
Parameter: A comparator whose ordering is to be reversed by the returned comparator(it can also be null)
Return Type: A comparator that imposes the reverse of the natural ordering on a collection of objects that implement theComparable interface.
1.To sort a list in descending order
2. To Sort an Array in Descending Order
3.To sort students in descending order of roll numbers when there is a user-defined comparator to do reverse.
1.To sort a list in descending order按降序对列表进行排序
public class Test2 {
public static void main(String[] args) {
List<Integer> al = Arrays.asList(1, 6, 8, 2, 4);//直接将数组转成ArrayList
// ArrayList<Integer> al = new ArrayList<Integer>();//或者使用这样的方法
// al.add(1);
// al.add(6);
// al.add(8);
// al.add(2);
// al.add(4);
// 使用 Collections 类的 sort() 方法,对元素和传递列表进行排序并使用,reverseOrder() 方法降序排序
Collections.sort(al, Collections.reverseOrder());
System.out.println( "List after the use of Collection.reverseOrder()"
+ " and Collections.sort() :\n" + al);
}
}
List after the use of Collection.reverseOrder() and Collections.sort() :
[8, 6, 4, 2, 1]
Arrays.sort()
cannot be used directly to sort primitive arrays in descending order.
If we try to call the Arrays.sort() method by passing reverse Comparator defined by Collections.reverseOrder(), it will throw the error .
2. To Sort an Array in Descending Order按降序对数组进行排序
public class Test2 {
public static void main(String[] args) {
Integer[] arr = { 30, 20, 40, 10 };
// Collections.sort 方法对arr[] 的元素降序排列,之后将 Arrays.sort() 应用于对数组进行排序
Arrays.sort(arr, Collections.reverseOrder());
System.out.println(
"Array after the use of Collection.reverseOrder()"
+ " and Arrays.sort() :\n"
+ Arrays.toString(arr));
}
}
Array after the use of Collection.reverseOrder() and Arrays.sort() :
[40, 30, 20, 10]
3.To sort students in descending order of roll numbers when there is a user-defined comparator to do reverse.当有用户定义的比较器做反向时,将学生按卷号降序排序。
public class Student {
int rollno;
String name, address;
public Student(int rollno, String name, String address) {
this.rollno = rollno;
this.name = name;
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"rollno=" + rollno +
", name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}
public class SortByRoll implements Comparator <Student> {
//用于按升序排序
public int compare(Student a, Student b){
return a.rollno-b.rollno;
}
public static void main(String[] args) {
ArrayList<Student> ar = new ArrayList<Student>();
ar.add(new Student(111, "bbbb", "london"));
ar.add(new Student(131, "aaaa", "nyc"));
ar.add(new Student(121, "cccc", "jaipur"));
System.out.println("Unsorted");
for (int i = 0; i < ar.size(); i++)
System.out.println(ar.get(i));
System.out.println("\nSorted by rollno");
Comparator c = Collections.reverseOrder(new SortByRoll());//按降序排列学生列表与 SortByRoll() 的升序被反过来
Collections.sort(ar,c);
for(int i =0;i<ar.size();i++){
System.out.println(ar.get(i));
}
}
}
Unsorted
Student{rollno=111, name='bbbb', address='london'}
Student{rollno=131, name='aaaa', address='nyc'}
Student{rollno=121, name='cccc', address='jaipur'}
Sorted by rollno
Student{rollno=131, name='aaaa', address='nyc'}
Student{rollno=121, name='cccc', address='jaipur'}
Student{rollno=111, name='bbbb', address='london'}