ENH: Let searchsorted work on arrays with descending values
See original GitHub issueQuite often arrays are sorted, either ascending or descending, and we want to find where the startpoint of a certain value is at. This is very easy for ascending arrays using searchsorted, but requires a bit more understanding for descending arrays.
It seems relatively straightward to it make it easy for descending arrays also by adding a ascending parameter to searchsorted.
Would this be outside of scope for numpy, or is such an ascending parameter relevant to include in numpy’s searchsorted?
A demonstration:
>>> import numpy as np
>>> def searchsorted(a, v, side='left', sorter=None, ascending=True):
>>> if not ascending:
>>> a = a[::-1]
>>> side = {'left': 'right', 'right': 'left'}[side]
>>> sorter = sorter[::-1] if sorter is not None else sorter
>>> return len(a) - np.searchsorted(a, v, side=side, sorter=sorter)
>>> return np.searchsorted(a, v, side=side, sorter=sorter)
>>> a = np.array([2, 1, 1])
>>> searchsorted(a, 1, ascending=False)
1
>>> searchsorted(a, 2, side='right', ascending=False)
1
``
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Numpy searchsorted descending order - Stack Overflow
Is searchsorted usable for arrays ordered in descending order? What options do I have if I need to keep the array in descending...
Read more >Working of the NumPy searchsorted() Function - eduCBA
The NumPy searchsorted() function can be used to find the index value where the new elements should be inserted in the sorted array...
Read more >numpy.searchsorted — NumPy v1.24 Manual
If sorter is None, then it must be sorted in ascending order, otherwise sorter must be an array of indices that sort it....
Read more >JavaScript Array Sort - W3Schools
You can use it to sort an array in descending order: ... By default, the sort() function sorts values as strings. This works...
Read more >numpy.searchsorted() - JAX documentation - Read the Docs
If sorter is None, then it must be sorted in ascending order, otherwise sorter must be an array of indices that sort it....
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
Perhaps
reverse=True
to match the builtinsorted
.I’m +1 on adding an argument to specify explicitly, but -1 on trying to auto-detect like
np.digitize
does.That probably depends on the solution. If just reversing the array and using the normal searchsorted code is enough, you can do that inside
array_searchsorted
. If it is not a good solution, you would need to modifyPyArray_SearchSorted
, but rename the new function so the function signature ofPyArray_SearchSorted
is not modified (the original would become just 1 line of code calling the new function).