从最高到最低对一个int数组进行排序 [英] Sorting an int array from highest to lowest

查看:68
本文介绍了从最高到最低对一个int数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我刚刚了解了Arrays.sort(arrayName);并将其应用到我的项目中,但是我发现它从最低到最高排序.反正有相反的做法吗? (我认为我不需要发布有问题的代码,但是如果需要,我会发布它.)

So I just learned about the Arrays.sort(arrayName); and applied it in a project of mine but I found it sorts from lowest to highest. Is there anyway to have it do the opposite? (I don't think I need to post the code in question but if it is needed I'll post it).

推荐答案

如果使用Integer[]而不是int[],则可以将Comparator作为第二个参数传递给sort方法.要强加反向顺序,可以使用 方法:

If you use an Integer[] instead of int[], then you can pass a Comparator as 2nd argument to the sort method. To impose reverse ordering, you can make use of Collections.reverseOrder() method:

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

这篇关于从最高到最低对一个int数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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