Sorting is a fundamental operation in data processing, and NumPy makes it efficient and intuitive with its powerful array sorting capabilities. Whether you're sorting numeric data, strings, or complex multi-dimensional arrays, NumPy provides optimized tools to get the job done quickly.
In this guide, you'll learn:
-
✅ What array sorting is in NumPy
-
How to use
numpy.sort()
,ndarray.sort()
, andargsort()
-
Sorting multi-dimensional arrays
-
Full code examples
-
Tips and Common pitfalls
Why Sort with NumPy?
Sorting helps in:
-
Preparing data for binary search or analysis
-
Organizing results for visualization
-
Cleaning or restructuring large datasets
NumPy’s built-in methods are much faster than Python’s list sorting, especially for large numerical datasets.
Basic Sorting with numpy.sort()
The most common function to sort arrays.
✅ Syntax:
numpy.sort(a, axis=-1, kind='quicksort', order=None)
-
a
: Array to sort -
axis
: Axis along which to sort -
kind
: Sorting algorithm —'quicksort'
,'mergesort'
,'heapsort'
,'stable'
-
order
: For structured arrays
✅ Example:
import numpy as np
arr = np.array([3, 1, 5, 2])
sorted_arr = np.sort(arr)
print(sorted_arr)
Output:
[1 2 3 5]
numpy.sort()
returns a sorted copy and does not modify the original array.
In-Place Sorting with ndarray.sort()
Sorts the array in place, modifying the original data.
arr = np.array([9, 7, 3, 1])
arr.sort()
print(arr)
Output:
[1 3 7 9]
Use this for memory efficiency when you don’t need the original array.
Getting Sorted Indices with numpy.argsort()
Returns the indices that would sort the array.
arr = np.array([40, 10, 20])
indices = np.argsort(arr)
print(indices)
Output:
[1 2 0]
You can use these indices to reorder another related array:
names = np.array(['apple', 'banana', 'cherry'])
sorted_names = names[indices]
print(sorted_names)
Sorting Multi-dimensional Arrays
✅ Sorting Row-wise (default):
arr = np.array([[3, 2, 1], [6, 5, 4]])
sorted_rows = np.sort(arr, axis=1)
print(sorted_rows)
Output:
[[1 2 3]
[4 5 6]]
✅ Sorting Column-wise:
sorted_cols = np.sort(arr, axis=0)
print(sorted_cols)
Output:
[[3 2 1]
[6 5 4]]
Sorting Structured Arrays by Field
If you're working with structured arrays (like CSVs or databases), you can sort by field.
data = np.array([(3, 'apple'), (1, 'banana'), (2, 'cherry')],
dtype=[('id', int), ('name', 'U10')])
sorted_data = np.sort(data, order='id')
print(sorted_data)
Output:
[(1, 'banana') (2, 'cherry') (3, 'apple')]
Full Working Code Example
import numpy as np
# 1D Sorting
arr1d = np.array([12, 4, 7, 9])
print("Sorted:", np.sort(arr1d))
# In-place sorting
arr1d.sort()
print("In-place Sorted:", arr1d)
# argsort usage
scores = np.array([88, 70, 96])
names = np.array(['Alice', 'Bob', 'Charlie'])
order = np.argsort(scores)
print("Sorted Names by Scores:", names[order])
# 2D Array sorting
arr2d = np.array([[9, 2, 4], [7, 1, 5]])
print("Row-wise Sort:\n", np.sort(arr2d, axis=1))
print("Column-wise Sort:\n", np.sort(arr2d, axis=0))
# Structured array sorting
data = np.array([(25, 'David'), (30, 'Anna')],
dtype=[('age', int), ('name', 'U10')])
print("Sorted by age:\n", np.sort(data, order='age'))
Tips and Best Practices
Tip | Benefit |
---|---|
Use argsort() for ranking or indirect sorting |
Great for sorting related arrays |
Specify axis to avoid confusion |
Ensures you sort along the right direction |
Use 'stable' for preserving order of equal elements |
Especially useful in time-series or categorical data |
Avoid modifying the original array unless necessary | Helps prevent accidental data loss |
Common Pitfalls
Pitfall | Solution |
---|---|
Forgetting axis in multi-dimensional arrays | Always specify axis explicitly |
Expecting np.sort() to modify in place |
Use .sort() for in-place sorting |
Using argsort() when you want actual sorted values |
Use sort() instead |
Conclusion
Sorting is one of the most common and powerful operations in data processing. NumPy provides multiple ways to:
-
Sort arrays (1D, 2D, structured)
-
Sort in place or return sorted copies
-
Retrieve sorted indices for indirect sorting
With the tools like sort()
, argsort()
, and sort(order=...)
, you're well-equipped to handle everything from simple sorting to advanced use cases in data science and machine learning.
Next Steps