Sorting in NumPy
Sorting is one of the most fundamental operations you’ll use when working with data. Whether you’re preparing data for analysis, ranking results, or cleaning up output, sorting lets you structure data in a meaningful way.
Why Sorting Matters
In real-world scenarios, datasets are often messy, unordered, or unsorted. NumPy’s sorting functions help you bring structure to that chaos — efficiently and elegantly. Sorting also plays a key role in search operations, data ranking, and statistical preprocessing.
Sorting 1D Arrays
Let’s start simple. We’ll sort a one-dimensional array using np.sort()
, the most straightforward method for sorting.
import numpy as np
arr = np.array([10, 3, 5, 7, 2])
sorted_arr = np.sort(arr)
print(sorted_arr)
[ 2 3 5 7 10]
Explanation:
np.sort()
returns a sorted copy of the array. The original array remains unchanged. It sorts in ascending order by default.
In-place Sorting with sort()
Method
If you want to modify the array directly without creating a new one, use the array method sort()
:
arr = np.array([6, 1, 8, 3])
arr.sort()
print(arr)
[1 3 6 8]
Note:
This changes the array in-place — be mindful when you don't want to lose the original order.
Sorting 2D Arrays
Sorting becomes more nuanced with multi-dimensional arrays. Let’s see how NumPy handles sorting along different axes.
matrix = np.array([[3, 7, 1],
[8, 2, 5]])
# Sort each row
sorted_rows = np.sort(matrix, axis=1)
print("Row-wise Sort:\n", sorted_rows)
# Sort each column
sorted_cols = np.sort(matrix, axis=0)
print("Column-wise Sort:\n", sorted_cols)
Row-wise Sort: [[1 3 7] [2 5 8]] Column-wise Sort: [[3 2 1] [8 7 5]]
Explanation:
Setting axis=1
sorts each row individually. axis=0
sorts each column. This behavior is intuitive: think “axis=1” as sorting across columns, and “axis=0” as sorting down rows.
Getting Sorted Indices with np.argsort()
Sometimes, you don't just want the sorted array — you want the indices that would sort it. That’s where argsort()
shines.
arr = np.array([40, 10, 20])
indices = np.argsort(arr)
print(indices)
print(arr[indices])
[1 2 0] [10 20 40]
Use Case:
argsort()
is useful when you want to sort one array and apply the same ordering to another related array (e.g., sorting scores and mapping back to names).
Sorting with Custom Order (Descending)
NumPy does not support direct descending sort using np.sort()
, but there’s a simple workaround using slicing:
arr = np.array([4, 1, 7, 3])
desc = np.sort(arr)[::-1]
print(desc)
[7 4 3 1]
By reversing the sorted array, we get descending order.
Things to Watch For
- In-place vs. Copy:
arr.sort()
modifies in place,np.sort(arr)
returns a new array. - Stability: NumPy's sort is stable — equal elements preserve their relative order.
- Data Type Awareness: Mixed types may produce unexpected results or errors.
- NaNs: By default,
np.sort
moves NaNs to the end (usenp.isnan
to filter them).
Verify Your Understanding
Before moving on, check your results:
- Use
print(arr)
before and after sorting to confirm the behavior. - Compare
np.sort()
vsarr.sort()
on the same array. - Try axis-based sorting on a 3x3 matrix — can you predict the result?
Conclusion
Sorting is more than just putting numbers in order — it’s a powerful tool for bringing structure to data. NumPy gives you flexibility, speed, and clarity through methods like sort()
, argsort()
, and axis-based sorting.
In the next section, we’ll look at how to search efficiently in sorted arrays using searchsorted()
. Once you can sort, search becomes far more effective.