1

For example, I have a numpy array like: a = np.array([0.5, 0.1, 0.5)].

When I use np.argsort(a)[::-1] to sort my array in descending order, it returns:

2, 0, 1

Which is technically correct, as np.argsort(a) would return:

1, 0, 2

and I am just reversing the order.

However, I want the sort to be in the order as the original array, so I would like the return to be:

0, 2, 1

How do I do that? Is there a better function than np.argsort()?

ashank
  • 11
  • 1
  • Does this answer your question? [Is it possible to use argsort in descending order?](https://stackoverflow.com/questions/16486252/is-it-possible-to-use-argsort-in-descending-order) – Ignatius Reilly Jul 29 '22 at 16:40

1 Answers1

2

I would try like this

np.argsort(-1 * a)
MaryRa
  • 463
  • 1
  • 4
  • There're two slightly more efficient solutions in [this duplicated answer](https://stackoverflow.com/a/16486305/15032126) (see the comments also) – Ignatius Reilly Jul 29 '22 at 16:44