Java:如何以相反的顺序对浮点数组进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1354326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java : How to sort an array of floats in reverse order?
提问by Frank
I use the following lines to sort an array of floats in reverse order, but I got an error message, what's wrong ?
我使用以下几行以相反的顺序对浮点数组进行排序,但我收到一条错误消息,怎么了?
float sortedData[]=new float[100];
...
Arrays.sort(sortedData,Collections.reverseOrder());
Error : cannot find symbol
错误:找不到符号
symbol : method sort(float[],java.util.Comparator) location: class java.util.Arrays Arrays.sort(sortedData,Collections.reverseOrder());
符号:方法 sort(float[],java.util.Comparator) 位置:class java.util.Arrays Arrays.sort(sortedData,Collections.reverseOrder());
=========================================================================
================================================== ========================
I was confused because in Jdk1.6 api, I saw this : [ Arrays ] public static void sort(float[] a), it doesn't say : public static void sort(Float[] a)
我很困惑,因为在 Jdk1.6 api 中,我看到了这个:[Arrays] public static void sort( float[] a),它没有说:public static void sort( Float[] a)
采纳答案by crunchdog
回答by Tom Hawtin - tackline
I suggest using a Arrays.sort(float[])and then writing a method reverse(float[])(you may or may not want to shift the NaNs around).
我建议使用 aArrays.sort(float[])然后编写一个方法reverse(float[])(您可能想也可能不想移动 NaN)。
回答by dfa
there is no Arrays.sort(float[], Comparator)method; however you can use or Arrays.asList()or just use a boxed Float[] array:
没有Arrays.sort(float[], Comparator)方法;但是,您可以使用或Arrays.asList()仅使用装箱的 Float[] 数组:
Float[] sortedData = new Float[100];
...
Arrays.sort(sortedData, Collections.reverseOrder());
In order to box a primitive array you can use the following code:
为了装箱一个原始数组,您可以使用以下代码:
public static Float[] floatArray(float... components) {
return toBoxedArray(Float.class, components);
}
private static <T> T[] toBoxedArray(Class<T> boxClass, Object components) {
final int length = Array.getLength(components);
Object res = Array.newInstance(boxClass, length);
for (int i = 0; i < length; i++) {
Array.set(res, i, Array.get(components, i));
}
return (T[]) res;
}
or include something like commons lang in your project and use ArrayUtils
回答by Sean Patrick Floyd
Guavahas a method Floats.asList()for creating a List<Float>backed by a float[]array. You can use this with Collections.sort to apply the Comparator to the underlying array.
Guava有一种Floats.asList()创建List<Float>由float[]数组支持的方法。您可以将它与 Collections.sort 一起使用,以将 Comparator 应用于底层数组。
List<Float> floatList = Floats.asList(arr);
Collections.sort(floatList, Collections.reverseOrder());
Note that the list is a live view backed by the actual array, so it should be pretty efficient.
请注意,该列表是由实际数组支持的实时视图,因此它应该非常有效。
回答by Paul Tomblin
The compiler isn't lying to you. There isn't a method in Arrays called "sort" that takes an array of float and a Comparator. There is, however, a method called "sort" which takes an array of Objects and a Comparator. Perhaps if you converted your array to an array of Float before you called sort?
编译器不会骗你。Arrays 中没有一种称为“sort”的方法,它接受一个浮点数组和一个比较器。但是,有一种称为“排序”的方法,它采用对象数组和比较器。也许如果您在调用 sort 之前将数组转换为 Float 数组?
Think of it as a defect in Java's autoboxing if you will, that it can't autobox an array of primitives into an array of the equivalent Objects.
如果愿意,可以将其视为 Java 自动装箱中的一个缺陷,即它无法将原语数组自动装箱为等效对象数组。
回答by Thorbj?rn Ravn Andersen
Others have explained why. Can you sort it first and then reverse it?
其他人已经解释了原因。能不能先排序再反转?
回答by Picrochole
The same, using autoboxing in the for-loop:
同样,在 for 循环中使用自动装箱:
double[] dbArray = {2.0, 3.0, 4.0, 5.0};
Double[] dBArray = new Double[dbArray.length];
int i = 0;
for(Double db : dbArray){
dBArray[i] = db; i++;
}
Arrays.sort(dBArray, Collections.reverseOrder());
回答by shailendra1118
With Java 8, you can make use of Steam APIs like below -
使用 Java 8,您可以使用 Steam API,如下所示 -
(There is no FloatStream and direct mapToFloat method but you can always use double)
(没有 FloatStream 和直接的 mapToFloat 方法,但您始终可以使用 double)
float[] unsorted = new float[100];
....
List<Double> sorted = IntStream.range(0, unsorted.length).mapToDouble(i->unsorted[i]).boxed().sorted(Collections.reverseOrder()).collect(Collectors.toList());
- Code using boxed method to converting primitive to object type.
- Collections.reverseOrder() aids to the sorting in reverse order.
- At last collecting it to list, here you can use other types as well.
- 使用装箱方法将原始类型转换为对象类型的代码。
- Collections.reverseOrder() 有助于以相反的顺序进行排序。
- 最后收集起来列出来,这里也可以使用其他类型。

