Use .argsort() it returns an numpy.array of indices that sort the given numpy.array. You call it as a function or as a method on your array. For example, suppose you have

import numpy as np

arr = np.array([[-0.30565392, -0.96605562],
                [ 0.85331367, -2.62963495],
                [ 0.87839643, -0.28283675],
                [ 0.72676698,  0.93213482],
                [-0.52007354,  0.27752806],
                [-0.08701666,  0.22764316],
                [-1.78897817,  0.50737573],
                [ 0.62260038, -1.96012161],
                [-1.98231706,  0.36523876],
                [-1.07587382, -2.3022289 ]])

You can now call .argsort() on the column you want to sort, and it will give you an array of row indices that sort that particular column which you can pass as an index to your original array.

>>> arr[arr[:, 1].argsort()]
array([[ 0.85331367, -2.62963495],
       [-1.07587382, -2.3022289 ],
       [ 0.62260038, -1.96012161],
       [-0.30565392, -0.96605562],
       [ 0.87839643, -0.28283675],
       [-0.08701666,  0.22764316],
       [-0.52007354,  0.27752806],
       [-1.98231706,  0.36523876],
       [-1.78897817,  0.50737573],
       [ 0.72676698,  0.93213482]])

You can equivalently use numpy.argsort()

>>> arr[np.argsort(arr[:, 1])]
array([[ 0.85331367, -2.62963495],
       [-1.07587382, -2.3022289 ],
       [ 0.62260038, -1.96012161],
       [-0.30565392, -0.96605562],
       [ 0.87839643, -0.28283675],
       [-0.08701666,  0.22764316],
       [-0.52007354,  0.27752806],
       [-1.98231706,  0.36523876],
       [-1.78897817,  0.50737573],
       [ 0.72676698,  0.93213482]])
Answer from JaminSore on Stack Overflow
🌐
Medium
medium.com › @whyamit404 › sorting-numpy-arrays-by-column-using-numpy-sort-c45a44660dcc
Sorting NumPy Arrays by Column Using numpy.sort() | by whyamit404 | Medium
February 26, 2025 - Here’s how you can use it: ... For column-wise sorting, you use axis=0. This means each column will be sorted independently. kind (optional): Lets you choose the sorting algorithm, like 'quicksort' or 'mergesort'.
🌐
ProjectPro
projectpro.io › recipes › sort-2d-array-by-column-numpy
How to sort a 2D array by a column in numpy? -
June 22, 2022 - This is Sample 2D array : [[100 101 500 104] [201 202 203 204] [301 300 600 307]] Index = 2 Array_sort = Sample_array[Sample_array[:,Index].argsort()] print("The original array is:","\n","\n", Sample_array, "\n") print("The sorted array is:", "\n", "\n", Array_sort)
🌐
thisPointer
thispointer.com › home › numpy › sorting 2d numpy array by column or row in python
Sorting 2D Numpy Array by column or row in Python - thisPointer
July 5, 2019 - For this we need to change positioning of all rows in 2D numpy array based on sorted values of 2nd column i.e. column at index 1. Let’s see how to do that, How to save Numpy Array to a CSV File using numpy.savetxt() in Python
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.ndarray.sort.html
numpy.ndarray.sort — NumPy v2.5 Manual
Sorting algorithm. The default is ‘quicksort’. Note that both ‘stable’ and ‘mergesort’ use timsort under the covers and, in general, the actual implementation will vary with datatype. The ‘mergesort’ option is retained for backwards compatibility. ... When a is an array with fields defined, this argument specifies which fields to compare first, second, etc.
🌐
Like Geeks
likegeeks.com › home › python › numpy › sorting numpy arrays: a comprehensive guide
Sorting NumPy Arrays: A Comprehensive Guide
Here, np.argsort() returns an array of indices that would sort the array. We then applied these indices to the array to obtain the sorted array. Let’s say we want to sort a 2D array based on the values in the first column, then by the values ...
🌐
Sharp Sight
sharpsight.ai › blog › numpy-sort
A quick guide to NumPy sort - Sharp Sight
February 6, 2024 - ... The code axis = 1 indicates that we’ll be sorting the data in the axis-1 direction, and by using the negative sign in front of the array name and the function name, the code will sort the rows in descending order.
🌐
Codefinity
codefinity.com › courses › v2 › 4f4826d5-e2f8-4ffd-9fd0-6f513353d70a › a4da9564-36a0-4109-b920-88fc7b89ddbb › e919a538-f041-447b-8082-e453a3c2aa5b
Learn Sorting 2D Arrays | Commonly used NumPy Functions
When sorting 2D arrays in descending order along a given axis, you need to use two slices: one full slice ([:]) and another with a negative step ([::-1]). The position of the slice with the negative step should correspond to the axis along which ...
Find elsewhere
🌐
IncludeHelp
includehelp.com › python › how-to-sort-a-2d-numpy-array-by-multiple-axes.aspx
Python - How to sort a 2D NumPy array by multiple axes?
# Import numpy import numpy as np # Creating a numpy array arr = np.array([(3, 2), (6, 2), (3, 6), (3, 4), (5, 3)]) # Display original array print("original array:\n",arr,"\n") # Sorting the array res = np.lexsort((arr[:,1],arr[:,0])) # Display result print("Result:\n",arr[res]) In this example, we have used the following Python basic topics that you should learn:
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_sort.asp
NumPy Sorting Arrays
The NumPy ndarray object has a function called sort(), that will sort a specified array.
🌐
Wellsr
wellsr.com › python › sorting-numpy-arrays-in-python
How to Sort NumPy Arrays in Python (with Examples) - wellsr.com
April 29, 2022 - The sort() method from the NumPy module is used to sort NumPy arrays in Python. You can pass the array that you want to sort to the sort() method.
Top answer
1 of 6
46

How does your "2D array" look like?

For example:

>>> a = [
     [12, 18, 6, 3], 
     [ 4,  3, 1, 2], 
     [15,  8, 9, 6]
]
>>> a.sort(key=lambda x: x[1])
>>> a
[[4,  3,  1, 2], 
 [15, 8,  9, 6], 
 [12, 18, 6, 3]]

But I guess you want something like this:

>>> a = [
     [12, 18, 6, 3], 
     [ 4,  3, 1, 2], 
     [15,  8, 9, 6]
]
>>> a = zip(*a)
>>> a.sort(key=lambda x: x[1])
>>> a
[(6,  1,  9), 
 (3,  2,  6), 
 (18, 3,  8), 
 (12, 4, 15)]
>>> a = zip(*a)
>>> a
[(6, 3, 18, 12), 
 (1, 2,  3,  4), 
 (9, 6,  8, 15)
]
2 of 6
23

Python, per se, has no "2d array" -- it has (1d) lists as built-ins, and (1d) arrays in standard library module array. There are third-party libraries such as numpy which do provide Python-usable multi-dimensional arrays, but of course you'd be mentioning such third party libraries if you were using some of them, rather than just saying "in Python", right?-)

So I'll assume that by "2d array" you mean a list of lists, such as:

lol = [ range(10), range(2, 12), range(5, 15) ]

or the like -- i.e. a list with 3 items, each item being a list with 10 items, and the "second row" would be the sublist item lol[1]. Yeah, lots of assumptions, but your question is so maddeningly vague that there's no way to avoid making assumptions - edit your Q to clarify with more precision, and an example!, if you dislike people trying to read your mind (and probably failing) as you currently make it impossible to avoid.

So under these assumptions you can sort each of the 3 sublists in the order required to sort the second one, for example:

indices = range(10)
indices.sort(key = lol[1].__getitem__)
for i, sublist in enumerate(lol):
  lol[i] = [sublist[j] for j in indices]

The general approach here is to sort the range of indices, then just use that appropriately sorted range to reorder all the sublists in play.

If you actually have a different problem, there will of course be different solutions;-).

🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.lexsort.html
numpy.lexsort — NumPy v2.5 Manual
To sort lexicographically with argsort, we would need to provide a structured array. >>> 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 ...
🌐
ProjectPro
projectpro.io › recipes › sort-array-by-nth-column
How to use NumPy to sort by column in an array- ProjectPro
October 19, 2023 - In this example, we're sorting the array by the second column (index 1). The sorted array will be stored in 'a'. ... This code efficiently sorts 'a' by its second column. You can adapt this code to sort by any other column of interest. For example, if you want to learn how to sort a NumPy 2D array by first column, use ‘0’ index instead of ‘1’ index.
🌐
CodingNomads
codingnomads.com › numpy-sort
NumPy Sort: How to Sort a NumPy Array
To sort arrays using NumPy sort, use the command np.sort() to return a sorted copy of an array without altering the original.
🌐
Python Guides
pythonguides.com › python-sort-numpy-array
Python Sort NumPy Array + 5 Examples
February 14, 2025 - NumPy & Pandas · Machine learning Follow the beginner roadmap · Python fundamentals · Each topic page collects every tutorial on that subject, with examples you can copy and run. intData Types · + -Operators · "ab"Strings · [ ]Lists · ( )Tuples · set()Sets · {k:v}Dictionaries · arrayArrays ·