According to the documentation

Returns the indices that would sort an array.

  • 2 is the index of 0.0.
  • 3 is the index of 0.1.
  • 1 is the index of 1.41.
  • 0 is the index of 1.48.
Answer from falsetru on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.5 Manual
>>> ind = np.unravel_index(np.argsort(x, axis=None), x.shape) >>> ind (array([0, 1, 1, 0]), array([0, 0, 1, 1])) >>> x[ind] # same as np.sort(x, axis=None) array([0, 2, 2, 3])
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-argsort-in-python
numpy.argsort() in Python - GeeksforGeeks
July 11, 2025 - import numpy as np a = np.array([[2, 0, 1], [5, 4, 3]]) print("Axis 0:\n", np.argsort(a, axis=0)) print("Axis 1:\n", np.argsort(a, axis=1)) ... Axis 0 (columns): The values in each column are compared top-to-bottom.
🌐
Programiz
programiz.com › python-programming › numpy › methods › argsort
NumPy argsort()
import numpy as np array = ... of the sorted array sortedIndices = np.argsort(array) print('Index of sorted array:', sortedIndices) print('Sorted array:', array[sortedIndices]) # Output # Index of sorted array: [3 1 2 0] # Sorted array: [-1 2 9 10]...
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.3 Manual
>>> ind = np.unravel_index(np.argsort(x, axis=None), x.shape) >>> ind (array([0, 1, 1, 0]), array([0, 0, 1, 1])) >>> x[ind] # same as np.sort(x, axis=None) array([0, 2, 2, 3])
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.2 Manual
>>> ind = np.unravel_index(np.argsort(x, axis=None), x.shape) >>> ind (array([0, 1, 1, 0]), array([0, 0, 1, 1])) >>> x[ind] # same as np.sort(x, axis=None) array([0, 2, 2, 3])
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to use numpy argsort() in python
How to Use NumPy Argsort() in Python - Spark By {Examples}
March 27, 2024 - NumPy argsort() function in Python is used to calculate an indirect sort along the specified axis using the algorithm specified by the kind keyword. It
🌐
Vultr Docs
docs.vultr.com › python › third party › numpy › argsort()
Python Numpy argsort() - Sort Array Indices
November 11, 2024 - Use the argsort() function to get sorted indices. ... import numpy as np data = np.array([10, 1, 5, 3, 8, 6]) sorted_indices = data.argsort() print(sorted_indices)
Top answer
1 of 10
175

According to the documentation

Returns the indices that would sort an array.

  • 2 is the index of 0.0.
  • 3 is the index of 0.1.
  • 1 is the index of 1.41.
  • 0 is the index of 1.48.
2 of 10
51

[2, 3, 1, 0] indicates that the smallest element is at index 2, the next smallest at index 3, then index 1, then index 0.

There are a number of ways to get the result you are looking for:

import numpy as np
import scipy.stats as stats

def using_indexed_assignment(x):
    "https://stackoverflow.com/a/5284703/190597 (Sven Marnach)"
    result = np.empty(len(x), dtype=int)
    temp = x.argsort()
    result[temp] = np.arange(len(x))
    return result

def using_rankdata(x):
    return stats.rankdata(x)-1

def using_argsort_twice(x):
    "https://stackoverflow.com/a/6266510/190597 (k.rooijers)"
    return np.argsort(np.argsort(x))

def using_digitize(x):
    unique_vals, index = np.unique(x, return_inverse=True)
    return np.digitize(x, bins=unique_vals) - 1

For example,

In [72]: x = np.array([1.48,1.41,0.0,0.1])

In [73]: using_indexed_assignment(x)
Out[73]: array([3, 2, 0, 1])

This checks that they all produce the same result:

x = np.random.random(10**5)
expected = using_indexed_assignment(x)
for func in (using_argsort_twice, using_digitize, using_rankdata):
    assert np.allclose(expected, func(x))

These IPython %timeit benchmarks suggests for large arrays using_indexed_assignment is the fastest:

In [50]: x = np.random.random(10**5)
In [66]: %timeit using_indexed_assignment(x)
100 loops, best of 3: 9.32 ms per loop

In [70]: %timeit using_rankdata(x)
100 loops, best of 3: 10.6 ms per loop

In [56]: %timeit using_argsort_twice(x)
100 loops, best of 3: 16.2 ms per loop

In [59]: %timeit using_digitize(x)
10 loops, best of 3: 27 ms per loop

For small arrays, using_argsort_twice may be faster:

In [78]: x = np.random.random(10**2)

In [81]: %timeit using_argsort_twice(x)
100000 loops, best of 3: 3.45 µs per loop

In [79]: %timeit using_indexed_assignment(x)
100000 loops, best of 3: 4.78 µs per loop

In [80]: %timeit using_rankdata(x)
100000 loops, best of 3: 19 µs per loop

In [82]: %timeit using_digitize(x)
10000 loops, best of 3: 26.2 µs per loop

Note also that stats.rankdata gives you more control over how to handle elements of equal value.

Find elsewhere
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.1 Manual
>>> ind = np.unravel_index(np.argsort(x, axis=None), x.shape) >>> ind (array([0, 1, 1, 0]), array([0, 0, 1, 1])) >>> x[ind] # same as np.sort(x, axis=None) array([0, 2, 2, 3])
🌐
Sharp Sight
sharpsight.ai › blog › numpy-argsort
How to Use Numpy Argsort in Python - Sharp Sight
April 10, 2022 - For example: np.argsort(myarray). If you don’t explicitly use the a= parameter, then the argsort function assumes that the first argument to the function is the input array to be passed to the a parameter. Additionally, the argsort function will accept my different data structures as inputs. Frequently, we’ll use a Numpy array as the input.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.6.dev0 Manual
>>> ind = np.unravel_index(np.argsort(x, axis=None), x.shape) >>> ind (array([0, 1, 1, 0]), array([0, 0, 1, 1])) >>> x[ind] # same as np.sort(x, axis=None) array([0, 2, 2, 3])
🌐
EDUCBA
educba.com › home › software development › software development tutorials › numpy tutorial › numpy.argsort()
Numpy.argsort() | Sorting the Algorithms for NumPy with Example
March 28, 2023 - Following is the representation in which code has to be drafted in the Python language for the application of the numpy argsort function: ... The argsort function is utilized to return and the indices representative of the array where the array ‘a’ is sorted along the axis which has been specified by the user. Following are the properties of the three variables with respect to the arguments for argsort(): ... Let us take some examples to understand n1.argsort() on various dimensionally oriented arrays to understanding the mechanism of sorting used:
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_argsort_function.htm
Numpy argsort() Function
Following is a basic example to find the indices that would sort a 1-dimensional NumPy array using the Python numpy.argsort() function −
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.0 Manual
>>> ind = np.unravel_index(np.argsort(x, axis=None), x.shape) >>> ind (array([0, 1, 1, 0]), array([0, 0, 1, 1])) >>> x[ind] # same as np.sort(x, axis=None) array([0, 2, 2, 3])
🌐
Javatpoint
javatpoint.com › numpy-argsort
numpy.argsort() in Python - Javatpoint
This function is used to create a ndarray by using an iterable object. It returns a one-dimensional ndarray object. Syntax numpy.fromiter(iterable, dtype, count = - 1) Parameters It accepts the following parameters. Iterable: It represents an iterable object.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.ma.argsort.html
numpy.ma.argsort — NumPy v2.2 Manual
ma.argsort(a, axis=<no value>, kind=None, order=None, endwith=True, fill_value=None, *, stable=None)[source]#
🌐
Python Guides
pythonguides.com › python-numpy-argsort
How To Use Np.argsort In Descending Order In Python
May 16, 2025 - It’s worth noting that NumPy also offers the option to specify the sort direction, but surprisingly, this feature isn’t available for argsort(), only for the regular sort() function. I hope you found this guide helpful! In this tutorial, I have explained two methods, such as using negative array values and using array slicing with[::-1]. I also covered a real-world example, performance considerations, when to use argsort() in Descending Order, and
🌐
Skytowner
skytowner.com › explore › numpy_argsort_method
NumPy | argsort method with Examples
Numpy's argsort(~) method returns the integer indices of the sorted copy of the input array.
🌐
Bomberbot
bomberbot.com › python › mastering-numpys-argsort-a-comprehensive-guide-for-python-data-wizards
Mastering NumPy's argsort: A Comprehensive Guide for Python Data Wizards - Bomberbot
Here's an example that demonstrates how to find the indices of the top 5 scores in a dataset: import numpy as np # Sample student scores scores = np.array([85, 92, 78, 95, 88, 91, 87, 79, 93, 86]) names = np.array(['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'Grace', 'Henry', 'Ivy', ...