4

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)

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Frank
  • 30,590
  • 58
  • 161
  • 244

8 Answers8

3

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
  • 13,078
  • 3
  • 23
  • 19
3

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
  • 145,806
  • 30
  • 211
  • 305
2

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
  • 114,442
  • 31
  • 189
  • 228
1

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
  • 179,021
  • 58
  • 319
  • 408
1

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.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • Floats.asList() links gives 404. May be you can use https://github.com/google/guava/blob/master/guava/src/com/google/common/primitives/Floats.java – baris.aydinoz Sep 17 '17 at 10:56
0

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

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
0

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());
Picrochole
  • 167
  • 5
0

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.