Write a Python code to sort an array in NumPy by the nth column?

In this article, we will show you how to sort an array in NumPy by the nth column both in ascending and descending order in Python.

NumPy is a Python library designed to work efficiently with arrays in Python. It is fast, simple to learn, and efficient in storage. NumPy arrays can be sorted by any column using the argsort() function, which returns the indices that would sort the array.

Method 1: Sorting by a Specific Column (Ascending)

The argsort() function performs an indirect sort and returns indices that would sort the array. We can use these indices to sort the entire array by any column ?

import numpy as np

# Creating a NumPy array with random numbers from 0 to 19
inputArray = np.random.randint(0, 20, (5, 5))

print("The input random array is:")
print(inputArray)

# Sorting by the 1st column (index 1)
print("Sorting the input array by the 1st column:")
sorted_array = inputArray[inputArray[:, 1].argsort()]
print(sorted_array)
The input random array is:
[[ 8 16  7  5 13]
 [ 5  8  6  0  2]
 [11  7  3  3  6]
 [ 5  3 15  7  5]
 [15  3  9 14  3]]
Sorting the input array by the 1st column:
[[ 5  3 15  7  5]
 [15  3  9 14  3]
 [11  7  3  3  6]
 [ 5  8  6  0  2]
 [ 8 16  7  5 13]]

Method 2: Sorting by the nth Column

You can sort by any column by changing the column index. Here's how to sort by a user-specified column ?

import numpy as np

# Creating a NumPy array with random numbers from 0 to 99
inputArray = np.random.randint(0, 100, (4, 4))

print("The input random array is:")
print(inputArray)

# Specifying the column to sort by (0-indexed)
n = 2  # Sort by 3rd column (index 2)

print(f"Sorting the input array by column {n}:")
sorted_array = inputArray[inputArray[:, n].argsort()]
print(sorted_array)
The input random array is:
[[32  5 68 67]
 [ 7  7 12 63]
 [49 49 10 15]
 [96  5 93 29]]
Sorting the input array by column 2:
[[49 49 10 15]
 [ 7  7 12 63]
 [32  5 68 67]
 [96  5 93 29]]

Method 3: Sorting in Descending Order

To sort in descending order, use slice notation [::-1] to reverse the sorted indices ?

import numpy as np

# Creating a NumPy array with random numbers
inputArray = np.random.randint(0, 100, (5, 5))

print("The input random array is:")
print(inputArray)

# Sort by column 3 in descending order
n = 3
print(f"Sorting the input array by column {n} in descending order:")
sorted_descending = inputArray[inputArray[:, n].argsort()[::-1]]
print(sorted_descending)
The input random array is:
[[47  6 62 53 50]
 [ 5 30 13 10 33]
 [43 93 57 91 91]
 [84 40 21 55  9]
 [76 89 85  3  4]]
Sorting the input array by column 3 in descending order:
[[43 93 57 91 91]
 [84 40 21 55  9]
 [47  6 62 53 50]
 [ 5 30 13 10 33]
 [76 89 85  3  4]]

How It Works

The syntax inputArray[inputArray[:, n].argsort()] works as follows:

  • inputArray[:, n] selects the nth column
  • argsort() returns the indices that would sort this column
  • inputArray[indices] reorders all rows based on these indices
  • [::-1] reverses the order for descending sort

Conclusion

Use argsort() with column indexing to sort NumPy arrays by any column. Add [::-1] for descending order. This method maintains row integrity while sorting by the specified column.

Updated on: 2026-03-26T22:24:42+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements