🧱 NumPy Array Sort – Sorting Arrays Efficiently in Python
🧲 Introduction – Why Learn Sorting in NumPy?
Sorting is a core operation in data analysis, used for ranking, organizing, and preprocessing data. Whether you’re preparing inputs for machine learning, ranking scores, or analyzing statistics, NumPy’s fast and flexible sorting tools such as np.sort()
, np.argsort()
, and sort()
method offer optimized ways to work with arrays of any shape.
🎯 In this guide, you’ll learn:
- How to sort 1D and multi-dimensional arrays using
np.sort()
- The difference between
sort()
,argsort()
, andlexsort()
- How to sort along specific axes
- Performance tips and common pitfalls
🔢 Using np.sort()
– The Default Sort Function
import numpy as np
arr = np.array([3, 1, 4, 1, 5, 9])
sorted_arr = np.sort(arr)
print(sorted_arr)
👉 Output:
[1 1 3 4 5 9]
✅ np.sort()
returns a sorted copy, leaving the original unchanged.
🔁 In-Place Sorting with .sort()
Method
arr = np.array([3, 1, 4, 1, 5, 9])
arr.sort()
print(arr)
👉 Output:
[1 1 3 4 5 9]
📌 .sort()
modifies the original array in-place and is slightly faster.
🧮 Sorting 2D Arrays with Axis Control
matrix = np.array([[3, 2, 1], [6, 5, 4]])
sorted_rows = np.sort(matrix, axis=1) # Sort each row
print(sorted_rows)
👉 Output:
[[1 2 3]
[4 5 6]]
sorted_cols = np.sort(matrix, axis=0) # Sort each column
print(sorted_cols)
👉 Output:
[[3 2 1]
[6 5 4]]
📌 axis=1
→ sort across columns (row-wise)
📌 axis=0
→ sort down rows (column-wise)
🧠 Using np.argsort()
– Indices that Would Sort the Array
arr = np.array([10, 5, 3])
indices = np.argsort(arr)
print(indices)
👉 Output:
[2 1 0]
✅ Useful when you want to sort related data using these indices:
sorted_arr = arr[indices]
📚 Using np.lexsort()
– Sorting by Multiple Keys
names = np.array(['John', 'Bob', 'Alice'])
grades = np.array([90, 80, 80])
# Sort by grade, then name
sorted_idx = np.lexsort((names, grades))
print(names[sorted_idx])
👉 Output:
['Alice' 'Bob' 'John']
📌 lexsort()
sorts by the last key first (grade here), then breaks ties with name.
🧮 Sorting Boolean and String Arrays
Boolean Arrays
arr = np.array([True, False, True])
print(np.sort(arr))
👉 Output:
[False True True]
String Arrays
arr = np.array(['banana', 'apple', 'cherry'])
print(np.sort(arr))
👉 Output:
['apple' 'banana' 'cherry']
✅ Works lexicographically
📊 NumPy Sort Method Comparison
Method | Description | Returns Copy? | Modifies Original? |
---|---|---|---|
np.sort() | General-purpose sort | ✅ Yes | ❌ No |
.sort() | In-place sort for arrays | ❌ No | ✅ Yes |
np.argsort() | Returns indices that sort the array | ✅ Yes | ❌ No |
np.lexsort() | Sort by multiple keys | ✅ Yes | ❌ No |
🚫 Common Pitfalls
- ❌ Confusing
sort()
withargsort()
- ❌ Forgetting that
np.sort()
doesn’t modify the array - ❌ Using incorrect axis on multi-dimensional arrays
- ❌ Assuming
argsort()
sorts the array – it only gives sort indices
🔍 Summary – Key Takeaways
- Use
np.sort()
for a copy,.sort()
for in-place sorting - Use
argsort()
to get indices for indirect sorting - Use
lexsort()
when sorting by multiple criteria - Specify
axis
to control row/column sorting - Sorting supports numeric, string, and boolean arrays
⚙️ Real-World Applications
- Sorting prediction probabilities
- Sorting names alphabetically in NLP
- Ranking scores in competitions
- Sorting image pixels for histogram equalization
- Sorting timestamps in chronological order
❓ FAQs – NumPy Array Sort
❓ What’s the difference between sort()
and argsort()
?
✅ sort()
returns sorted data; argsort()
returns the index positions.
❓ How do I sort each row of a matrix?
✅ Use:
np.sort(arr, axis=1)
❓ Can I sort in descending order?
✅ Yes:
np.sort(arr)[::-1] # or use -arr for numeric arrays
❓ How do I sort by two arrays (like keys)?
✅ Use np.lexsort((secondary_key, primary_key))
❓ Can I sort strings and booleans?
✅ Yes. NumPy sorts them in lexicographical or logical order.
Share Now :