简体   繁体   English

按降序对基本类型的数组进行排序

[英]Sorting arrays of primitive types in descending order

In Java, how do I sort a float[] array in reverse order with Arrays.sort()? 在Java中,如何使用Arrays.sort()以相反的顺序对float []数组进行排序? Or at least some function using the same algorithm. 或者至少使用相同算法的某些功能。

Please avoid any kind of comments about using Float instead of float . 请避免使用Float而不是float任何评论。 Also reversing the array after it has been sorted isn't an option either as it's a very critical piece of code. 在对数组进行排序后对其进行反转也不是一种选择,因为它是一段非常关键的代码。

来自Apache Commons的Javas Arrays.sort()ArrayUtils.reverse()

I suggest taking the source code from Arrays.sort(float[]), copy it in a new method in your project, then modify it in the relevant place to reverse (negate) the comparison result. 我建议从Arrays.sort(float [])获取源代码,将其复制到项目中的新方法中,然后在相关位置修改它以反转(否定)比较结果。

It should return exactly what you wanted - a reverse-sorted array, using exactly the same algorithm. 它应该返回你想要的 - 一个反向排序的数组,使用完全相同的算法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM