ENH: Reverse parameter in np.sort()
See original GitHub issueCurrently, in order to sort an array in reverse order you have to use some form of strange hack, which may or may not be guaranteed to work in the future.
I suggest adding a reverse parameter to np.sort, similar to the native Python sort() function. This would make the hacks unnecessary and improve readability of code, by making the sorting order explicit.
This feature should not result in any performance problems - after all, the sorting function simply have to reverse the boolean result when comparing values.
Reproducing code example:
import numpy as np
a = np.arange(10)
np.random.shuffle(a)
a.sort(reverse = True)
print(a) # should print "[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]"
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Can we sort Numpy arrays in reverse order? - Python FAQ
Answer. In Numpy, the np. sort() function does not allow us to sort an array in descending order. Instead, we can reverse an...
Read more >Efficiently sorting a numpy array in descending order?
It doesn't look like np.sort accepts parameters to change the sign of the comparisons in the sort operation to get things in reverse...
Read more >ENH: adding .unique() to DF (or return_inverse for duplicated ...
When deduplicating, I need to get the mapping from the deduplicated records back to the originals. Unfortunately: drop_duplicates and ...
Read more >Supported NumPy features - Numba
The following methods of Numpy arrays are supported in their basic form (without any optional arguments):. all() · any() · argmax() · argmin()...
Read more >The Python range() Function (Guide)
It does essentially the same thing but uses different parameters. With np.linspace() , you specify start and end (both inclusive) as well as...
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 Free
Top 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
This approach does not result in a stable sort
Interestingly, the here “descending” is used. I am a bit uncertain about that choice, since it is at odds with Pythons
sorted
andsort
.In general, we should probably solve this in C, this may require adding more sorting “methods” in C, which was not possible for a long time.
However, NEP 41+43 now makes it possible in a pretty straight-forward manner. I could help with that first step (extending the machinery, or at least providing a blueprint for it), but I don’t think I can commit time to the feature itself. If anyone can make it a their larger project, please let me know so I can help with that first step. (It is pretty new API, so there is no direct blueprint, me making a start on it would probably safe a lot of time for anyone diving into it.)