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 is wrong?

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

Error: cannot find character

character: 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 does not say: public static void sort ( Float [] a)

+4
source share
8 answers

This particular method accepts an array of type Object. The float type does not extend the Object class, but the Float .

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

I suggest using Arrays.sort(float[]) and then write the reverse(float[]) method (you may or may not want to shift NaN around).

+3
source

no Arrays.sort(float[], Comparator) method Arrays.sort(float[], Comparator) ; however, you can use either Arrays.asList() or just use the array Float []:

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

To set up 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

+2
source

The compiler does not lie to you. Arrays do not have a method called "sort" that accepts an array of float and Comparator. However, there is a method called "sorting" that takes an array of objects and a comparator. Maybe if you converted your array to a Float array before you called the sort?

Think of it as a defect in autoboxing Java if you want it to not be able to automatically use an array of primitives in an array of equivalent objects.

+1
source

Guava has a Floats.asList() method to create a List<Float> supported by the float[] array. You can use this with Collections.sort to apply a comparator to the underlying array.

 List<Float> floatList = Floats.asList(arr); Collections.sort(floatList, Collections.reverseOrder()); 

Note that the list is a live view supported by the actual array, so it should be pretty efficient.

+1
source

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

0
source

Same thing using autoboxing in a 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()); 
0
source

With Java 8, you can use the Steam APIs as shown below -

(There is no FloatStream method or 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()); 
  • Code using the boxed method to convert a primitive to an object type.
  • Collections.reverseOrder () helps sort in the reverse order.
  • Finally, collecting it in a list, here you can also use other types.
0
source

All Articles