Akira
Akira

Reputation: 2870

What is the sorting direction (ascending or descending) of numpy.argsort?

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?

Upvotes: 1

Views: 5126

Answers (1)

oskros
oskros

Reputation: 3285

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

Upvotes: 3

Related Questions