views:

1045

answers:

5

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)

+1  A: 

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.

Paul Tomblin
+2  A: 

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

dfa
`Arrays.asList` on `float[]` ain't gonna work (as you expect)!
Tom Hawtin - tackline
(And I really wouldn't go for reflection.)
Tom Hawtin - tackline
(Also doesn't to type checking for zero-length arrays.)
Tom Hawtin - tackline
feel free to change the code; this is only a sample, for "real" needs use ArraysUtils from common lang
dfa
-1 for relying on boxing and reflection. It's much easier and better performant to just sort the array normally and then reverse it.
Hosam Aly
+1  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());
crunchdog
If you're using `float`s, you probably have a performance reason for doing so.
Tom Hawtin - tackline
@Tom Hawtin, hardly in the year 2009? Only if you juggle A LOT of floats, and I do mean a lot.
crunchdog
+2  A: 

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).

Tom Hawtin - tackline
A: 

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

Thorbjørn Ravn Andersen