question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add reverse sorting for primitives

See original GitHub issue

It’s not actually easy to sort an int[] in reverse in Java. The typical ways you’d sort an array in reverse don’t work on primitives:

 Arrays.sort(array, Collections.reverseOrder()); // no comparators for primitives

 Arrays.sort(array);
 Collections.reverse(Arrays.asList(array)); // view array as a List and reverse it, but Arrays.asList doesn't work for int[]

The shortest way I can think of is

 Arrays.sort(array);
 Collections.reverse(Ints.asList(array));

…which boxes every element in the array.

Perhaps we should consider e.g. Ints.reverseSort or the like.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
Maaartinuscommented, Sep 7, 2017

@lowasser Maybe there should be Ints.reverse(int[])? Then a two step solution would be fast.

2reactions
JakeWhartoncommented, Sep 8, 2017

That’s what the classpath exemption is for.

On Fri, Sep 8, 2017 at 3:52 PM Jonathan Bluett-Duncan < notifications@github.com> wrote:

Edit: On the other hand, that is quite a bit of baggage to add, might as well use https://github.com/mintern-java/primitive.

@thekeenant https://github.com/thekeenant On the other hand, that library looks like it’s licensed under the GPLv2 + Class Exception, which doesn’t sound great to me for projects except GPL-licensed ones.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/google/guava/issues/2936#issuecomment-328198178, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEZXdAvDJj3s4jjaXEbot8YXiAtolks5sgZrjgaJpZM4PPEa8 .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sort large arrays of primitive types in descending order
The first approach that probably comes to mind is to convert it to a list of objects (boxing): double[] array = new double[1048576];...
Read more >
How to sort an Array in descending order in Java? Example ...
The only way to sort a primitive array in descending order is first to sort the array in ascending order and then reverse...
Read more >
Sort an array in descending order in Java | Techie Delight
The following code demonstrates this by applying the Collections.reverseOrder() comparator to the underlying primitive array. 1. 2. 3.
Read more >
Sort arrays of primitive types in descending order - Edureka
I got a huge array of double types. How can I sort elements in descending order? ... double[] array = new double[1048576]; Arrays.stream(array)....
Read more >
Sorting Arrays in Java - Baeldung
Sorting a primitive array in descending order is not quite as simple as sorting it in ascending order because Java doesn't support the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found