Java Collections.reverseOrder()详解

文章展示了如何在Java中使用Collections.reverseOrder()方法对List、Array以及自定义对象列表进行降序排序。示例包括对Integer列表、整数数组以及按学生学号排序的学生对象列表的排序操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 the Comparable 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'}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值