In this article we will discuss how to sort a 2D Numpy array by single or multiple rows or columns.

Sorting 2D Numpy Array by a column

First of all import numpy module i.e.

import numpy as np

Now suppose we have a 2D Numpy array i.e.
# Create a 2D Numpy array list of list
arr2D = np.array([[11, 12, 13, 22], [21, 7, 23, 14], [31, 10, 33, 7]])

print('2D Numpy Array')
print(arr2D)

Output:
2D Numpy Array
[[11 12 13 22]
 [21  7 23 14]
 [31 10 33  7]]

Now suppose we want to sort this 2D numpy array by 2nd column like this,
[[21  7 23 14]
 [31 10 33  7]
 [11 12 13 22]]

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,

Sorting 2D Numpy Array by column at index 1

columnIndex = 1
# Sort 2D numpy array by 2nd Column
sortedArr = arr2D[arr2D[:,columnIndex].argsort()]

print('Sorted 2D Numpy Array')
print(sortedArr)

Output:
Sorted 2D Numpy Array
[[21  7 23 14]
 [31 10 33  7]
 [11 12 13 22]]

It sorted the 2D Numpy array by 2nd column i.e. column at index 1.

How did it worked ?
Let’s break down the above expression part by part and understand how ot worked.

Select the column at index 1 from 2D numpy array i.e.

arr2D[:,columnIndex]

It returns the values at 2nd column i.e. column at index position 1 i.e.
[12 7 10]

Now get the array of indices that sort this column i.e.
arr2D[:,columnIndex].argsort()

It returns the index positions that can sort the above column i.e.
[1 2 0]

It means for sorting column at index position 1 use following order of rows : [1 2 0]
So, to change the positioning of rows based on values returned by argsort(). Pass that to [] operator of 2D numpy array i.e.
arr2D[arr2D[:,columnIndex].argsort()]

It will change the row order and make the 2D numpy array sorted by 2nd column i.e. by column at index position 1.

Let’s see some other examples,

Sorting 2D Numpy Array by column at index 0

# Sort 2D numpy array by first column
sortedArr = arr2D[arr2D[:,0].argsort()]

print('Sorted 2D Numpy Array')
print(sortedArr)

Output:
Sorted 2D Numpy Array
[[11 12 13 22]
 [21  7 23 14]
 [31 10 33  7]]

Sorting 2D Numpy Array by last column

# Sort 2D numpy array by last column
sortedArr = arr2D[arr2D[:, -1].argsort()]

print('Sorted 2D Numpy Array')
print(sortedArr)

Output:
[[31 10 33  7]
 [21  7 23 14]
 [11 12 13 22]]

Sort a 2D Numpy Array by row

On the similar logic we can sort a 2D Numpy array by a single row i.e. shuffle the columns of 2D numpy array to make the given row sorted.

Let’s understand by examples,

Suppose we have a 2D Numpy array i.e.

# Create a 2D Numpy array list of list
arr2D = np.array([[11, 12, 13, 22], [21, 7, 23, 14], [31, 10, 33, 7]])

print('2D Numpy Array')
print(arr2D)

Output:
2D Numpy Array
[[11 12 13 22]
 [21  7 23 14]
 [31 10 33  7]]

Sorting 2D Numpy Array by row at index position 1

Let’s sort the above created 2D Numpy array by 2nd row i.e. row at index position 1 i.e.

# Sort 2D numpy array by 2nd row
sortedArr = arr2D [ :, arr2D[1].argsort()]

print('Sorted 2D Numpy Array')
print(sortedArr)

Output:
Sorted 2D Numpy Array
[[12 22 11 13]
 [ 7 14 21 23]
 [10  7 31 33]]

It changed the positions of all columns in 2D numpy array to make row at index position 1 sorted. So, basically we sorted the 2D Numpy array by row at index 1.

How it worked ?

Its logic was similar to above i.e. Select row at given index position using [] operator and then get sorted indices of this row using argsort().

<New positioning of columns> = arr[rowindex].argsort()

Then change the positioning of columns to make this 2d array sorted by row.
array[:, <New positioning of columns>]

Some other examples,

Sorting 2D Numpy Array by First Row

# Sort 2D numpy array by 2nd row
sortedArr = arr2D [ :, arr2D[0].argsort()]

print('Sorted 2D Numpy Array')
print(sortedArr)

Output:
Sorted 2D Numpy Array
[[11 12 13 22]
 [21  7 23 14]
 [31 10 33  7]]

Sorting 2D Numpy Array by Last row

# Sort 2D numpy array by last row
sortedArr = arr2D[:, arr2D[-1].argsort()]

print('Sorted 2D Numpy Array')
print(sortedArr)

Output:
Sorted 2D Numpy Array
[[22 12 11 13]
 [14  7 21 23]
 [ 7 10 31 33]]

Complete example is as follows:
import numpy as np

def main():

  # Create a 2D Numpy array list of list
  arr2D = np.array([[11, 12, 13, 22], [21, 7, 23, 14], [31, 10, 33, 7]])

  print('2D Numpy Array')
  print(arr2D)

  print('****** Sort 2D Numpy array by column ******')

  print('*** Sort 2D Numpy array by 2nd column i.e. column at index 1 ***')

  columnIndex = 1
  # Sort 2D numpy array by 2nd Column
  sortedArr = arr2D[arr2D[:,columnIndex].argsort()]

  print('Sorted 2D Numpy Array')
  print(sortedArr)

  print('*** Sort 2D Numpy array by 1st column i.e. column at index 0 ***')

  # Sort 2D numpy array by first column
  sortedArr = arr2D[arr2D[:,0].argsort()]

  print('Sorted 2D Numpy Array')
  print(sortedArr)

  print('*** Sort 2D Numpy array by last column ***')

  # Sort 2D numpy array by last column
  sortedArr = arr2D[arr2D[:, -1].argsort()]

  print('Sorted 2D Numpy Array')
  print(sortedArr)

  print('****** Sort 2D Numpy array by row ******')

  # Create a 2D Numpy array list of list
  arr2D = np.array([[11, 12, 13, 22], [21, 7, 23, 14], [31, 10, 33, 7]])

  print('2D Numpy Array')
  print(arr2D)

  print('*** Sort 2D Numpy array by 2nd Row i.e. row at index position 1 ***')

  # Sort 2D numpy array by 2nd row
  sortedArr = arr2D [ :, arr2D[1].argsort()]

  print('Sorted 2D Numpy Array')
  print(sortedArr)

  print('*** Sort 2D Numpy array by First Row***')

  # Sort 2D numpy array by first row
  sortedArr = arr2D [ :, arr2D[0].argsort()]

  print('Sorted 2D Numpy Array')
  print(sortedArr)

  print('*** Sort 2D Numpy array by Last Row***')

  # Sort 2D numpy array by last row
  sortedArr = arr2D[:, arr2D[-1].argsort()]

  print('Sorted 2D Numpy Array')
  print(sortedArr)

if __name__ == '__main__':
  main()


Output:
2D Numpy Array
[[11 12 13 22]
 [21  7 23 14]
 [31 10 33  7]]
****** Sort 2D Numpy array by column ******
*** Sort 2D Numpy array by 2nd column i.e. column at index 1 ***
Sorted 2D Numpy Array
[[21  7 23 14]
 [31 10 33  7]
 [11 12 13 22]]
*** Sort 2D Numpy array by 1st column i.e. column at index 0 ***
Sorted 2D Numpy Array
[[11 12 13 22]
 [21  7 23 14]
 [31 10 33  7]]
*** Sort 2D Numpy array by last column ***
Sorted 2D Numpy Array
[[31 10 33  7]
 [21  7 23 14]
 [11 12 13 22]]
****** Sort 2D Numpy array by row ******
2D Numpy Array
[[11 12 13 22]
 [21  7 23 14]
 [31 10 33  7]]
*** Sort 2D Numpy array by 2nd Row i.e. row at index position 1 ***
Sorted 2D Numpy Array
[[12 22 11 13]
 [ 7 14 21 23]
 [10  7 31 33]]
*** Sort 2D Numpy array by First Row***
Sorted 2D Numpy Array
[[11 12 13 22]
 [21  7 23 14]
 [31 10 33  7]]
*** Sort 2D Numpy array by Last Row***
Sorted 2D Numpy Array
[[22 12 11 13]
 [14  7 21 23]
 [ 7 10 31 33]]