Apply numpy.argsort on flattened array and then unravel the indices back to (3, 3) shape:

>>> arr = np.array([[5, 2, 4],
[3, 3, 3],
[6, 1, 2]])
>>> np.dstack(np.unravel_index(np.argsort(arr.ravel()), (3, 3)))
array([[[2, 1],
        [0, 1],
        [2, 2],
        [1, 0],
        [1, 1],
        [1, 2],
        [0, 2],
        [0, 0],
        [2, 0]]])
Answer from Ashwini Chaudhary on Stack Overflow
Discussions

python - How to using numpy.argsort on a 2D array to sort another 2D array - Stack Overflow
I use numpy.argsort all the time for 1D data, but it seems to behaving differently in 2D. For example, let's say I want to argsort this array along axis 1 so the items in each row are in ascending ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Does numba not support np.argsort for 2d arrays? - Numba - Numba Discussion
Numba reports an error when running the np.argsort function of a 2-dimensional array. The 1d array runs normally. Is it not supported? from numba import njit from numba.types import unicode_type import numpy as np @njiโ€ฆ More on numba.discourse.group
๐ŸŒ numba.discourse.group
0
October 24, 2023
python - argsort for a multidimensional ndarray - Stack Overflow
That's just converting the array to a (2,1,3) right... why does that facilitate the i slicing? โ€“ DilithiumMatrix Commented Oct 15, 2015 at 13:58 ... I expanded my explanation. ... is there a simpler way of doing this? i thought the argsort should have considered what it's going to be used ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
November 20, 2018
python - Sorting 2D numpy array using indices returned from np.argsort() - Stack Overflow
7 Sort invariant for numpy.argsort with multiple dimensions ยท 12253 How can I remove a specific item from an array in JavaScript? More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GitHub
github.com โ€บ numpy โ€บ numpy โ€บ issues โ€บ 4724
argsort does not work for multidimensional arrays ยท Issue #4724 ยท numpy/numpy
consider the following code: a = np.random.random([5,5]) ind = np.argsort(a, axis=1) a_sorted = a[ind] np.sort(a, axis=1) now a_sorted and a should be both sorted along the 1-axis. However not even the shapes are identical anymore. a is ...
๐ŸŒ
Sharp Sight
sharpsight.ai โ€บ blog โ€บ numpy-argsort
How to Use Numpy Argsort in Python - Sharp Sight
April 10, 2022 - So using np.argsort() with axis = 1 causes the function to operate horizontally along the rows. The output is the row-wise index, if the input array were sorted along axis-1. Letโ€™s do one more example.
Find elsewhere
๐ŸŒ
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. ... The input array. ... The axis along which to sort the input array. For 2D arrays, the allowed values are as follows:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ numpy-argsort-in-python
numpy.argsort() in Python - GeeksforGeeks
April 25, 2025 - The sorted array is obtained by indexing the original array a with the sorted indices. numpy.argsort(arr, axis=-1, kind='quicksort', order=None)
๐ŸŒ
SciPy
docs.scipy.org โ€บ doc โ€บ โ€บ numpy-1.12.0 โ€บ reference โ€บ generated โ€บ numpy.argsort.html
numpy.argsort โ€” NumPy v1.12 Manual
... Describes sorting algorithms used. ... 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.
๐ŸŒ
Numba Discussion
numba.discourse.group โ€บ t โ€บ does-numba-not-support-np-argsort-for-2d-arrays โ€บ 2240
Does numba not support np.argsort for 2d arrays? - Numba - Numba Discussion
October 24, 2023 - Numba reports an error when running the np.argsort function of a 2-dimensional array. The 1d array runs normally. Is it not supported? from numba import njit from numba.types import unicode_type import numpy as np @njit def foo(arr): return np.argsort(arr) foo(np.random.randn(3, 4).astype('float32'))
Top answer
1 of 4
14

Solution:

>>> a[np.arange(np.shape(a)[0])[:,np.newaxis], np.argsort(a)]
array([[1, 2, 3],
       [2, 8, 9]])

You got it right, though I wouldn't describe it as cheating the indexing.

Maybe this will help make it clearer:

In [544]: i=np.argsort(a,axis=1)

In [545]: i
Out[545]: 
array([[1, 2, 0],
       [2, 0, 1]])

i is the order that we want, for each row. That is:

In [546]: a[0, i[0,:]]
Out[546]: array([1, 2, 3])

In [547]: a[1, i[1,:]]
Out[547]: array([2, 8, 9])

To do both indexing steps at once, we have to use a 'column' index for the 1st dimension.

In [548]: a[[[0],[1]],i]
Out[548]: 
array([[1, 2, 3],
       [2, 8, 9]])

Another array that could be paired with i is:

In [560]: j=np.array([[0,0,0],[1,1,1]])

In [561]: j
Out[561]: 
array([[0, 0, 0],
       [1, 1, 1]])

In [562]: a[j,i]
Out[562]: 
array([[1, 2, 3],
       [2, 8, 9]])

If i identifies the column for each element, then j specifies the row for each element. The [[0],[1]] column array works just as well because it can be broadcasted against i.

I think of

np.array([[0],
          [1]])

as 'short hand' for j. Together they define the source row and column of each element of the new array. They work together, not sequentially.

The full mapping from a to the new array is:

[a[0,1]  a[0,2]  a[0,0]
 a[1,2]  a[1,0]  a[1,1]]

def foo(a):
    i = np.argsort(a, axis=1)
    return (np.arange(a.shape[0])[:,None], i)

In [61]: foo(a)
Out[61]: 
(array([[0],
        [1]]), array([[1, 2, 0],
        [2, 0, 1]], dtype=int32))
In [62]: a[foo(a)]
Out[62]: 
array([[1, 2, 3],
       [2, 8, 9]])
2 of 4
6

The above answers are now a bit outdated, since new functionality was added in numpy 1.15 to make it simpler; take_along_axis (https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.take_along_axis.html) allows you to do:

>>> a = np.array([[3,1,2],[8,9,2]])
>>> np.take_along_axis(a, a.argsort(axis=-1), axis=-1)
array([[1 2 3]
       [2 8 9]])
๐ŸŒ
Omz Software
omz-software.com โ€บ pythonista โ€บ numpy โ€บ reference โ€บ generated โ€บ numpy.argsort.html
numpy.argsort โ€” NumPy v1.8 Manual
... Describes sorting algorithms used. ... 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
... Describes sorting algorithms used. ... 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.
๐ŸŒ
Finxter
blog.finxter.com โ€บ np-argsort
np.argsort() โ€” A Simpe Illustrated Guide โ€“ Be on the Right Side of Change
July 3, 2022 - We can simply add the [::-1] to the output of the np.argsort() function to get a descending order sorted index. So far, weโ€™ve only seen some 1d array examples. In this part, I will show you how to deploy the axis argument with some 2d array examples! By the way, you can always use the ...
๐ŸŒ
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 also apply argsort() with a two-dimensional array along with the specified axis, for that, you have to pass the axis parameter along with the array. Now you can specify axis=0, then this function will return the column-wise indices of ...