8

Surprisingly, it seems there was no simple, one-liner kind of solution in java to sort int array in descending order before java 8. For example, check this post. Now that we have java 8, is there an elegant, simple, one-liner way using java 8 features, such as stream and lambda expression, to sort an int array in descending order?

Edit
I am interested in a solution for int[], not Integer[].

Edit
I am interested in a solution that only uses JAVA SE library.

2
  • 1
    Possible duplicate: Sort arrays of primitive types in descending order Commented Aug 3, 2015 at 1:19
  • 1
    You can always use Arrays.sort() and then iterate the array in reverse order... Commented Aug 3, 2015 at 17:10

3 Answers 3

13

With guava you could simply write

Ints.asList(a).sort(Comparator.reverseOrder());

It may be not so efficient since it requires boxing int to Integer, but it is elegant one-liner.

You can also write something like

int[] sorted = IntStream.of(a)
        .boxed()
        .sorted(Comparator.reverseOrder())
        .mapToInt(i -> i)
        .toArray();

but this also suffers from boxing and it needs to create new array.

Anyway I doubt you will find nice solution in standard Java free of boxing since Comparator<T> can accept only objects. For now best way would be using Arrays.sort and reverse its order manually.

Sign up to request clarification or add additional context in comments.

1 Comment

In my StreamEx library there's a shortcut for this: IntStreamEx.of(a).reverseSorted().toArray() (which does the same boxing/unboxing internally). In JDK there's no ready algorithm to sort primitive array with custom comparator, so it's not possible to do this without intermediate boxing (or some third-party library which implements such primitive sorting).
5
int[] arr = ...;
Arrays.sort(arr);
int[] reversed = IntStream.range(0, arr.length)
                          .map(i -> arr[arr.length-i-1])
                          .toArray();

is probably the closest you could do if you don't want to box the int into its respective wrapper class for each value in the array.

If you suffer from performances by doing the sort once (O(nlogn)) and the reverse operation after (O(n)), you might want to look into Arrays.parallelSort and parallelize the IntStream.

1 Comment

or IntStream.range(0, arr.length/2).forEach(i -> {int tmp = arr[i]; arr[i] = arr[arr.length-i-1]; arr[arr.length-i-1] = tmp;}); if you want to reverse in place in arr directly.
0

Variant without boxing:

int[] sorted = IntStream.of(a)
    .map( i -> -i)
    .sorted()
    .map( i -> -i)
    .toArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.