NumPy Sorting
NumPy provides efficient and flexible sorting functions that allow you to order arrays, find rankings, and organize data. Whether you need to sort values, get sorted indices, or apply custom sort logic, NumPy's sorting API covers a variety of use cases in data processing and scientific computing.
Key Features of NumPy's Sorting Functions
- Fast and efficient: Sorting operations are implemented in C for high performance on large arrays.
- Multiple algorithms: Choose from quicksort, mergesort, heapsort, and stable sorting methods.
- Axis-specific sorting: Sort along any axis of multi-dimensional arrays.
Understanding how to use NumPy’s sort(), argsort(), and related functions will help you handle array data more effectively in your numerical and data analysis workflows.
Basic Sorting with np.sort()
The simplest way to sort an array in NumPy is to use the np.sort() function. It returns a sorted copy of the array, leaving the original array unchanged. By default, it sorts in ascending order.
import numpy as np
arr = np.array([4,8,1,6,4,9,2,4,5])
# Sort the array in ascending order
sorted_arr = np.sort(arr)
print("Original:", arr)
print("Sorted:", sorted_arr)
arr.sort()
print('sorted original array:', arr)
import numpy as np
arr = np.array([4,8,1,6,4,9,2,4,5])
# Sort the array in ascending order
sorted_arr = np.sort(arr)
print("Original:", arr)
print("Sorted:", sorted_arr)
arr.sort()
print('sorted original array:', arr)
How It Works:
- np.sort() returns a sorted copy of the array.
- The original array is not modified unless you use sort() as a method (e.g., arr.sort()).
- By default, sorting is in ascending order.
- You can use arr.sort() to sort the array in-place without returning a new array.
Output
Original: [4 8 1 6 4 9 2 4 5]
Sorted: [1 2 4 4 4 5 6 8 9]
sorted original array: [1 2 4 4 4 5 6 8 9]
Original: [4 8 1 6 4 9 2 4 5]
Sorted: [1 2 4 4 4 5 6 8 9]
sorted original array: [1 2 4 4 4 5 6 8 9]
💡 Tip: Use np.sort() when you want a new sorted array without altering the original data.
Use arr.sort() for in-place sorting when memory efficiency is important.
Sorting with the axis Parameter
When working with multi-dimensional arrays, you can control how sorting is applied using the axis parameter. This determines whether sorting is performed across rows or columns.
Sorting a 2D Array by Axis
import numpy as np
arr = np.array([[4, 5, 1],
[6, 2, 3]])
# Sort each row (axis=1)
sorted_by_row = np.sort(arr, axis=1)
# Sort each column (axis=0)
sorted_by_col = np.sort(arr, axis=0)
# Flatten and sort entire array (axis=None)
sorted_flat = np.sort(arr, axis=None)
print("Original:\n", arr)
print("Sorted by row (axis=1):\n", sorted_by_row)
print("Sorted by column (axis=0):\n", sorted_by_col)
print("Sorted flat array:", sorted_flat)
import numpy as np
arr = np.array([[4, 5, 1],
[6, 2, 3]])
# Sort each row (axis=1)
sorted_by_row = np.sort(arr, axis=1)
# Sort each column (axis=0)
sorted_by_col = np.sort(arr, axis=0)
# Flatten and sort entire array (axis=None)
sorted_flat = np.sort(arr, axis=None)
print("Original:\n", arr)
print("Sorted by row (axis=1):\n", sorted_by_row)
print("Sorted by column (axis=0):\n", sorted_by_col)
print("Sorted flat array:", sorted_flat)
How It Works
- axis=1 sorts each row individually (i.e. across columns).
- axis=0 sorts each column individually (i.e. down rows).
- axis=None flattens the array before sorting, treating all elements as a 1D array.
Output
Original:
[[4 5 1]
[6 2 3]]
Sorted by row (axis=1):
[[1 4 5]
[2 3 6]]
Sorted by column (axis=0):
[[4 2 1]
[6 5 3]]
Sorted flat array: [1 2 3 4 5 6]
Original:
[[4 5 1]
[6 2 3]]
Sorted by row (axis=1):
[[1 4 5]
[2 3 6]]
Sorted by column (axis=0):
[[4 2 1]
[6 5 3]]
Sorted flat array: [1 2 3 4 5 6]
💡 Tip: Always specify the axis explicitly when working with 2D or higher-dimensional arrays to avoid confusion and ensure correct sorting behavior.
Sorting with the kind Parameter
The kind parameter in np.sort() lets you choose the sorting algorithm. Different algorithms have different performance characteristics and behaviors, especially when dealing with equal elements or structured data.
Available Sorting Algorithms
- 'quicksort' (default): Fastest for most cases, but not stable (equal items may not retain their original order).
- 'mergesort': A stable sort. Useful when order consistency is important, like with structured arrays.
- 'heapsort': A memory-efficient option but generally slower than quicksort or mergesort.
- 'stable': Alias for mergesort. Guarantees stable sorting behavior.
Example: Comparing Sorting Algorithms
import numpy as np
arr = np.array([4, 7, 9, 1])
# Using different sorting kinds
sorted_quick = np.sort(arr, kind='quicksort')
sorted_merge = np.sort(arr, kind='mergesort')
sorted_heap = np.sort(arr, kind='heapsort')
sorted_stable = np.sort(arr, kind='stable')
print("Original: ", arr)
print("Quicksort result:", sorted_quick)
print("Mergesort result:", sorted_merge)
print("Heapsort result:", sorted_heap)
print("Stable result: ", sorted_stable)
import numpy as np
arr = np.array([4, 7, 9, 1])
# Using different sorting kinds
sorted_quick = np.sort(arr, kind='quicksort')
sorted_merge = np.sort(arr, kind='mergesort')
sorted_heap = np.sort(arr, kind='heapsort')
sorted_stable = np.sort(arr, kind='stable')
print("Original: ", arr)
print("Quicksort result:", sorted_quick)
print("Mergesort result:", sorted_merge)
print("Heapsort result:", sorted_heap)
print("Stable result: ", sorted_stable)
Output
Original: [4 7 9 1]
Quicksort result: [1 4 7 9]
Mergesort result: [1 4 7 9]
Heapsort result: [1 4 7 9]
Stable result: [1 4 7 9]
Original: [4 7 9 1]
Quicksort result: [1 4 7 9]
Mergesort result: [1 4 7 9]
Heapsort result: [1 4 7 9]
Stable result: [1 4 7 9]
💡 Tip: If you need to maintain the relative order of equal elements (e.g., in structured or labeled data), use kind='stable' or 'mergesort'.
Sorting in Descending Order
NumPy's np.sort() function sorts in ascending order by default and does not have a built-in descending parameter. However, you can easily sort in descending order by reversing the sorted array using slicing.
Method 1: Using Slicing ([::-1])
import numpy as np
arr = np.array([4, 1, 6, 3])
# Sort in ascending order, then reverse for descending
descending = np.sort(arr)[::-1]
print("Descending:", descending)
import numpy as np
arr = np.array([4, 1, 6, 3])
# Sort in ascending order, then reverse for descending
descending = np.sort(arr)[::-1]
print("Descending:", descending)
Output
Descending: [6 4 3 1]
Descending: [6 4 3 1]
Frequently Asked Questions
How do I sort an array in NumPy?
How do I sort an array in NumPy?
Use np.sort() to get a sorted copy of an array, or use the in-place ndarray.sort() method to modify the original array.
What is the difference between sort() and argsort()?
What is the difference between sort() and argsort()?
sort() returns a sorted array, whereas argsort() returns the indices that would sort the array. You can use these indices to reorder arrays or extract sorted order information.
How do I sort a 2D array by rows or columns?
How do I sort a 2D array by rows or columns?
Use the axis parameter: axis=1 sorts each row, and axis=0 sorts each column.
How can I perform a descending sort in NumPy?
How can I perform a descending sort in NumPy?
NumPy does not have a direct descending sort option. Instead, sort ascending and then reverse the array with slicing like arr[::-1]. Alternatively, for numeric arrays, multiply by -1 before sorting.
What sorting algorithms does NumPy support?
What sorting algorithms does NumPy support?
NumPy supports "quicksort", "mergesort", "heapsort", and "stable" via the kind parameter for the sort() function.
What's Next?
Up next, we’ll dive into argsort and lexsort in NumPy — powerful tools for obtaining sorted indices and performing multi-key sorting. You'll learn how to efficiently sort arrays based on one or multiple criteria.