🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.4 Manual
Indirect stable sort with multiple keys. ... Inplace sort. ... Indirect partial sort. ... Apply index_array from argsort to an array as if by calling sort.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.5.dev0 Manual
Indirect stable sort with multiple keys. ... Inplace sort. ... Indirect partial sort. ... Apply index_array from argsort to an array as if by calling sort.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.1 Manual
Indirect stable sort with multiple keys. ... Inplace sort. ... Indirect partial sort. ... Apply index_array from argsort to an array as if by calling sort.
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.3 Manual
Indirect stable sort with multiple keys. ... Inplace sort. ... Indirect partial sort. ... Apply index_array from argsort to an array as if by calling sort.
🌐
Omz Software
omz-software.com › pythonista › numpy › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v1.8 Manual
Indirect stable sort with multiple keys. ... Inplace sort. ... Indirect partial sort. ... See sort for notes on the different sorting algorithms. As of NumPy 1.4.0 argsort works with real/complex arrays containing nan values.
🌐
SciPy
docs.scipy.org › doc › numpy-1.9.2 › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v1.9 Manual
Indirect stable sort with multiple keys. ... Inplace sort. ... Indirect partial sort. ... See sort for notes on the different sorting algorithms. As of NumPy 1.4.0 argsort works with real/complex arrays containing nan values.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.2 Manual
Indirect stable sort with multiple keys. ... Inplace sort. ... Indirect partial sort. ... Apply index_array from argsort to an array as if by calling sort.
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.0 Manual
Indirect stable sort with multiple keys. ... Inplace sort. ... Indirect partial sort. ... Apply index_array from argsort to an array as if by calling sort.
Find elsewhere
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.lexsort.html
numpy.lexsort — NumPy v2.5.dev0 Manual
>>> x = np.array([(ai, bi) for ai, bi in zip(a, b)], ... dtype = np.dtype([('x', int), ('y', int)])) >>> np.argsort(x) # or np.argsort(x, order=('x', 'y')) array([2, 0, 4, 6, 5, 3, 1]) The zeroth axis of keys always corresponds with the sequence of keys, so 2D arrays are treated just like other ...
🌐
GeeksforGeeks
geeksforgeeks.org › numpy-argsort-in-python
numpy.argsort() in Python - GeeksforGeeks
April 25, 2025 - Explanation: A 1D NumPy array is created and displayed. np.argsort(a) returns the indices that would sort the array. The sorted array is obtained by indexing the original array a with the sorted indices.
🌐
SciPy
docs.scipy.org › doc › › numpy-1.11.0 › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v1.11 Manual
Indirect stable sort with multiple keys. ... Inplace sort. ... Indirect partial sort. ... See sort for notes on the different sorting algorithms. As of NumPy 1.4.0 argsort works with real/complex arrays containing nan values.
🌐
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 - You can sort a 2-D array in descending order using numpy.argsort(). The key is to first get the sorted indices in ascending order, and then reverse these indices to obtain the descending order.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.ma.argsort.html
numpy.ma.argsort — NumPy v2.2 Manual
Only for compatibility with np.argsort. Ignored. ... Array of indices that sort a along the specified axis. In other words, a[index_array] yields a sorted a. ... Describes sorting algorithms used. ... Indirect stable sort with multiple keys.
🌐
SciPy
docs.scipy.org › doc › numpy-1.15.1 › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v1.15 Manual
Indirect stable sort with multiple keys. ... Inplace sort. ... Indirect partial sort. ... See sort for notes on the different sorting algorithms. As of NumPy 1.4.0 argsort works with real/complex arrays containing nan values.
🌐
Programiz
programiz.com › python-programming › numpy › methods › argsort
NumPy argsort()
import numpy as np array = np.array([10, 2, 9, 1]) # sort an array in ascending order by quicksort algorithm array2 = np.argsort(array, kind = 'quicksort') print('Index of sorted array:\n',array2) print('Sorted array:\n',array[array2]) ... quicksort (default): This is a fast algorithm that works well for most cases i.e. small and medium-sized arrays with random or uniformly distributed elements.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.sort.html
numpy.sort — NumPy v2.4 Manual
NaT now sorts to the end of arrays for consistency with NaN. ... Try it in your browser! >>> import numpy as np >>> a = np.array([[1,4],[3,1]]) >>> np.sort(a) # sort along the last axis array([[1, 4], [1, 3]]) >>> np.sort(a, axis=None) # sort the flattened array array([1, 1, 3, 4]) >>> np.sort(a, axis=0) # sort along the first axis array([[1, 1], [3, 4]]) Use the order keyword to specify a field to use when sorting a structured array:
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.lexsort.html
numpy.lexsort — NumPy v2.4 Manual
dtype = np.dtype([('x', int), ('y', int)])) >>> np.argsort(x) # or np.argsort(x, order=('x', 'y')) array([2, 0, 4, 6, 5, 3, 1]) The zeroth axis of keys always corresponds with the sequence of keys, so 2D arrays are treated just like other sequences of keys. >>> arr = np.asarray([b, a]) >>> ind2 = np.lexsort(arr) >>> np.testing.assert_equal(ind2, ind) Accordingly, the axis parameter refers to an axis of each key, not of the keys argument itself.
Top answer
1 of 10
174

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.

🌐
Sharp Sight
sharpsight.ai › blog › numpy-argsort
How to Use Numpy Argsort in Python - Sharp Sight
April 10, 2022 - This tutorial explains how to use the Numpy argsort function. It explains the syntax of np.argsort, and also shows clear examples. If you need help with something specific, you can click on any of these links.