Java数组降序排序? [英] Java Array Sort descending?

查看:30
本文介绍了Java数组降序排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么简单的方法可以按降序对数组进行排序,就像在 数组类?

Is there any EASY way to sort an array in descending order like how they have a sort in ascending order in the Arrays class?

或者我必须停止懒惰并自己做这个:[

Or do I have to stop being lazy and do this myself :[

推荐答案

你可以用它来对所有类型的对象进行排序

You could use this to sort all kind of Objects

sort(T[] a, Comparator<? super T> c) 

Arrays.sort(a, Collections.reverseOrder());

Arrays.sort() 不能直接用于按降序对原始数组进行排序.如果您尝试通过传递由 Collections.reverseOrder() 定义的反向比较器来调用 Arrays.sort() 方法,则会抛出错误

Arrays.sort() cannot be used directly to sort primitive arrays in descending order. If you try to call the Arrays.sort() method by passing reverse Comparator defined by Collections.reverseOrder() , it will throw the error

没有找到适合 sort(int[],comparator) 的方法

no suitable method found for sort(int[],comparator)

这适用于对象数组",例如整数数组,但不适用于原始数组,例如 int 数组.

That will work fine with 'Array of Objects' such as Integer array but will not work with a primitive array such as int array.

按降序对原始数组进行排序的唯一方法是,先按升序对数组进行排序,然后将数组原地反转.对于二维原始数组也是如此.

The only way to sort a primitive array in descending order is, first sort the array in ascending order and then reverse the array in place. This is also true for two-dimensional primitive arrays.

这篇关于Java数组降序排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆