Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Array Sort descending?

People also ask

How do you put an array in descending order in Java?

To sort an array in Java in descending order, you have to use the reverseOrder() method from the Collections class. The reverseOrder() method does not parse the array. Instead, it will merely reverse the natural ordering of the array.

Is array sorted descending?

The only way to sort a primitive array in descending order is, first sort the array in ascending order and then reverse the array in place. This is also true for two-dimensional primitive arrays. Convert your primitives to their respective objects. Integer for int, Double for double, Boolean for boolean, etc.

How do you sort an array element in descending order?

Use the Reverse() method that would eventually give you a sorted array in descending order. Array. Reverse(list); You can try to run the following code to to sort an array in descending order.

How do you sort an array in ascending and descending order in Java?

We can sort arrays in ascending order using the sort() method which can be accessed from the Arrays class. The sort() method takes in the array to be sorted as a parameter. To sort an array in descending order, we used the reverseOrder() method provided by the Collections class.


You could use this to sort all kind of Objects

sort(T[] a, Comparator<? super T> c) 

Arrays.sort(a, Collections.reverseOrder());

Arrays.sort() cannot be used directly to sort primitive arrays in descending order. If you try to call the Arrays.sort() method by passing reverse Comparator defined by Collections.reverseOrder() , it will throw the error

no suitable method found for sort(int[],comparator)

That will work fine with 'Array of Objects' such as Integer array but will not work with a primitive array such as int array.

The only way to sort a primitive array in descending order is, first sort the array in ascending order and then reverse the array in place. This is also true for two-dimensional primitive arrays.


for a list

Collections.sort(list, Collections.reverseOrder());

for an array

Arrays.sort(array, Collections.reverseOrder());

You can use this:

    Arrays.sort(data, Collections.reverseOrder());

Collections.reverseOrder() returns a Comparator using the inverse natural order. You can get an inverted version of your own comparator using Collections.reverseOrder(myComparator).


an alternative could be (for numbers!!!)

  1. multiply the Array by -1
  2. sort
  3. multiply once again with -1

Literally spoken:

array = -Arrays.sort(-array)

without explicit comparator:

Collections.sort(list, Collections.reverseOrder());

with explicit comparator:

Collections.sort(list, Collections.reverseOrder(new Comparator()));

It's not directly possible to reverse sort an array of primitives (i.e., int[] arr = {1, 2, 3};) using Arrays.sort() and Collections.reverseOrder() because those methods require reference types (Integer) instead of primitive types (int).

However, we can use Java 8 Stream to first box the array to sort in reverse order:

// an array of ints
int[] arr = {1, 2, 3, 4, 5, 6};

// an array of reverse sorted ints
int[] arrDesc = Arrays.stream(arr).boxed()
    .sorted(Collections.reverseOrder())
    .mapToInt(Integer::intValue)
    .toArray();

System.out.println(Arrays.toString(arrDesc)); // outputs [6, 5, 4, 3, 2, 1]

Java 8:

Arrays.sort(list, comparator.reversed());

Update: reversed() reverses the specified comparator. Usually, comparators order ascending, so this changes the order to descending.