Guava: Add reverse sorting for primitives

Created on 7 Sep 2017  Â·  5Comments  Â·  Source: google/guava

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.

package=primitives status=fixed type=addition type=enhancement

Most helpful comment

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

All 5 comments

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

If there is reverse sorting, I would think there should be an interface for implementing your own comparator (IntComparator, FloatComparator, etc.) which behave like a Comparator, but for primitives.

Ints.sort(new int[] {5, 2, 3, 1}, Ints::compare); // IntComparator can be functional

There would need to be an implementation of sorting (mergesort, quicksort) which utilizes the custom comparator interface - one for each primitive potentially.

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

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 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.

That's what the classpath exemption is for.

On Fri, Sep 8, 2017 at 3:52 PM Jonathan Bluett-Duncan <
[email protected]> 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
.

Note that fastutil has IntArrays#quickSort(array, IntComparator). Similar support is provided for other primitive types plus other sort strategies. For those who work with primitives for performance reasons, they may already be using libraries dedicated towards that purpose.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PhilippWendler picture PhilippWendler  Â·  4Comments

terence1984 picture terence1984  Â·  3Comments

cpovirk picture cpovirk  Â·  5Comments

dongritengfei picture dongritengfei  Â·  3Comments

gissuebot picture gissuebot  Â·  3Comments