Add reverse sorting for primitives
See original GitHub issueIt’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:
- Created 6 years ago
- Reactions:4
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@lowasser Maybe there should be
Ints.reverse(int[])
? Then a two step solution would be fast.That’s what the classpath exemption is for.
On Fri, Sep 8, 2017 at 3:52 PM Jonathan Bluett-Duncan < notifications@github.com> wrote: