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

19,010

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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.

Solution 5

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.

Share:
19,010
Frank
Author by

Frank

I've been a Java developer for over 20 years. Recently got a patent for "Interception-resistant Authentication System And Method" : GATE [ Graphic Access Tabular Entry ]. GATE makes user passwords much safer, it helps you to defeat peeking, keylogging, phishing  and dictionary attack. Info : https://gatecybertech.com Demo : https://gatecybertech.net GATE has won multiple cyber security awards. If any organization wants to license it, please contact me.

Updated on July 12, 2022

Comments

  • Frank
    Frank almost 2 years

    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)

  • Brett
    Brett over 14 years
    Arrays.asList on float[] ain't gonna work (as you expect)!
  • Brett
    Brett over 14 years
    (And I really wouldn't go for reflection.)
  • Brett
    Brett over 14 years
    If you're using floats, you probably have a performance reason for doing so.
  • Brett
    Brett over 14 years
    (Also doesn't to type checking for zero-length arrays.)
  • dfa
    dfa over 14 years
    feel free to change the code; this is only a sample, for "real" needs use ArraysUtils from common lang
  • Hosam Aly
    Hosam Aly over 14 years
    -1 for relying on boxing and reflection. It's much easier and better performant to just sort the array normally and then reverse it.
  • crunchdog
    crunchdog over 14 years
    @Tom Hawtin, hardly in the year 2009? Only if you juggle A LOT of floats, and I do mean a lot.
  • baris.aydinoz
    baris.aydinoz over 6 years
    Floats.asList() links gives 404. May be you can use github.com/google/guava/blob/master/guava/src/com/google/com‌​mon/…