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.

ENH: Reverse parameter in np.sort()

See original GitHub issue

Currently, 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:open
  • Created 4 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
eric-wiesercommented, Jul 22, 2020

I just wanted to chime in here that you can already get reversed sorting without any copies with ndarray.sort and indexing:

This approach does not result in a stable sort

0reactions
sebergcommented, Jun 29, 2022

Interestingly, the here “descending” is used. I am a bit uncertain about that choice, since it is at odds with Pythons sorted and sort.

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

Read more comments on GitHub >

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

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