简体   繁体   中英

Java : How to sort an array of floats in reverse order?

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());

=========================================================================

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)

That specific method , takes an array of type Object. The type float does not extend the Object class, but Float does.

Float sortedData[]=new Float[100];
...
Arrays.sort(sortedData,Collections.reverseOrder());

我建议使用Arrays.sort(float[])然后编写一个reverse(float[])方法reverse(float[]) (你可能想也可能不想转移NaNs)。

there is no Arrays.sort(float[], Comparator) method; however you can use or Arrays.asList() or just use a boxed Float[] array:

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

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?

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.

Guava has 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.

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.

Others have explained why. Can you sort it first and then reverse it?

The same, using autoboxing in the for-loop:

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());

With Java 8, you can make use of Steam APIs like below -

(There is no FloatStream and direct mapToFloat method but you can always use 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());
  1. Code using boxed method to converting primitive to object type.
  2. Collections.reverseOrder() aids to the sorting in reverse order.
  3. At last collecting it to list, here you can use other types as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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