1

The function numpy.argsort seems to return sorted 1D-array in increasing order. For example,

import numpy as np
x = np.array([3, 1, 2])
ind = np.argsort(x)
x[ind]

returns array([1, 2, 3]). However, I've read through its documentation but could not find this information.

Could you please explain how to get it?

Akira
  • 2,594
  • 3
  • 20
  • 45

1 Answers1

2

So most sorting algorithms sort in ascending order if nothing else is specified. You can always just reverse the output yourself to get the sorting in descending order

import numpy as np
x = np.array([3, 1, 2])
ascending = np.argsort(x)
descending = ascending[::-1]

For more information on sorting direction of np.argsort, you can have a look at this post Is it possible to use argsort in descending order?

EDIT: I found a reference to the sort order of numpy here: https://numpy.org/doc/stable/reference/generated/numpy.sort.html#numpy.sort where it is mentioned that sorting is done in lexicographic order by default

oskros
  • 3,101
  • 2
  • 9
  • 28
  • 1
    This does not answer the question – RandomGuy May 19 '21 at 08:53
  • @RandomGuy I explain the algorithm is sorting in ascending order, and how to reverse it in case you want - that seems to me to answer the question. What do you miss in an answer? – oskros May 19 '21 at 08:54
  • He asked for where to find this information in the documentation, not how to reverse the `ind` array (which is quite trivial) – RandomGuy May 19 '21 at 08:56
  • @RandomGuy sure, but that information is not part of numpys documentation - likely because sorting algorithms default to using ascending order – oskros May 19 '21 at 09:00
  • 1
    @RandomGuy nevermind, actually found a reference to using lexicographic order in the `numpy.sort` documentation - thanks for being persistant haha – oskros May 19 '21 at 09:03