1

Say I have array:

A = [3, 3, 2, 2, 4]

The indices of the sorted list (ascending) would be:

[2, 3, 0, 1, 4]

I want the reverse sorted list (descending):

[4, 0, 1, 2, 3]

Note that the reverse sorted list is not a direct reverse of the sorted list.

I would have used numpy; however, if I do this:

indices = np.argsort(A)[::-1]

# This gives: indices = [4, 1, 0, 3, 2]
# But I want: indices = [4, 0, 1, 2, 3]

Thus, how do I get the indices of the reverse sorted list that I want? Or is there some way to do this with numpy?

Thanks!

Vick Conan
  • 43
  • 5

1 Answers1

1

You can np.argsort(..) the negative array here, like:

>>> np.argsort(-np.array([3, 3, 2, 2, 4]), kind='mergesort')
array([4, 0, 1, 2, 3])

or since , you can use:

>>> np.argsort(-np.array([3, 3, 2, 2, 4]), kind='stable')
array([4, 0, 1, 2, 3])
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555