[Numpy-discussion] searchsorted descending arrays

Nathaniel Smith njs at pobox.com
Mon May 6 19:16:41 EDT 2013

On Mon, May 6, 2013 at 6:54 PM, Bago <mrbago at gmail.com> wrote:
> I submitted a patch a little while ago,
> https://github.com/numpy/numpy/pull/3107, which gave the searchsorted
> function the ability to search arrays sorted in descending order. At the
> time my  approach was to detect the sortorder of the array by comparing the
> first and last elements. This works pretty well in most cases, but fails in
> one notable case. After giving it some thought, I think the best way to add
> searching of descending arrays to numpy would be by adding a keyword to the
> searchsorted function. I wanted to know what you guys thought of this before
> updating the pr.
> I would like to add something like the following to numpy:
> A = [10, 9, 2, 1]
> np.searchsorted(A, 5, sortorder='descending')
> the other option would be to auto-detect the order, but then this case might
> surprise some users:
> A = [0, 0, 0]
> A = np.sort(A)[::-1]
> print np.searchsorted(A, [1, -1])
> # [3, 0]
> This might surprise a user who expects to be searching a descending array

I agree, that result would not really be acceptable (could easily
break various algorithms in very hard to notice cases), so a kwarg
would be better.


More information about the NumPy-Discussion mailing list