Introduction
Sorting arrays is a common operation in data processing and analysis. NumPy provides efficient and versatile functions to sort arrays in various ways. In this chapter, you will learn different methods available in NumPy for sorting arrays, including sorting along different axes and using custom sorting criteria.
Creating a NumPy Array
Before we begin sorting, let’s create some sample NumPy arrays.
import numpy as np
# Create a sample 1D NumPy array
array_1d = np.array([3, 1, 2, 5, 4])
print("1D Array:\n", array_1d)
# Create a sample 2D NumPy array
array_2d = np.array([[3, 2, 1], [6, 5, 4], [9, 8, 7]])
print("2D Array:\n", array_2d)
Output:
1D Array:
[3 1 2 5 4]
2D Array:
[[3 2 1]
[6 5 4]
[9 8 7]]
Sorting a 1D Array
You can sort a 1D array using the np.sort()
function.
Example: Sorting a 1D Array
# Sort the 1D array
sorted_array_1d = np.sort(array_1d)
print("Sorted 1D Array:\n", sorted_array_1d)
Output:
Sorted 1D Array:
[1 2 3 4 5]
Sorting a 2D Array
You can sort a 2D array along different axes using the axis
parameter in the np.sort()
function.
Example: Sorting a 2D Array Along Rows
# Sort the 2D array along rows (axis=1)
sorted_array_2d_rows = np.sort(array_2d, axis=1)
print("2D Array Sorted Along Rows:\n", sorted_array_2d_rows)
Output:
2D Array Sorted Along Rows:
[[1 2 3]
[4 5 6]
[7 8 9]]
Example: Sorting a 2D Array Along Columns
# Sort the 2D array along columns (axis=0)
sorted_array_2d_cols = np.sort(array_2d, axis=0)
print("2D Array Sorted Along Columns:\n", sorted_array_2d_cols)
Output:
2D Array Sorted Along Columns:
[[3 2 1]
[6 5 4]
[9 8 7]]
In-Place Sorting
You can sort an array in-place using the sort
method of the array object.
Example: In-Place Sorting
# In-place sort the 1D array
array_1d.sort()
print("In-Place Sorted 1D Array:\n", array_1d)
Output:
In-Place Sorted 1D Array:
[1 2 3 4 5]
Custom Sorting Criteria
You can sort an array based on custom criteria using the np.argsort()
function, which returns the indices that would sort an array.
Example: Custom Sorting Criteria
# Create a custom sort order for the 1D array
custom_order = np.array([3, 1, 4, 2, 5])
sorted_indices = np.argsort(custom_order)
sorted_array_custom = array_1d[sorted_indices]
print("Custom Sorted 1D Array:\n", sorted_array_custom)
Output:
Custom Sorted 1D Array:
[2 3 1 4 5]
Sorting Structured Arrays
NumPy also allows sorting of structured arrays, where you can specify which fields to sort by.
Example: Sorting Structured Arrays
# Create a structured array
data = np.array([('John', 32, 75.5), ('Doe', 28, 82.1), ('Alice', 25, 65.2)],
dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])
# Sort by age
sorted_data = np.sort(data, order='age')
print("Sorted Structured Array by Age:\n", sorted_data)
Output:
Sorted Structured Array by Age:
[('Alice', 25, 65.2) ('Doe', 28, 82.1) ('John', 32, 75.5)]
Complete Example
Here is a complete example demonstrating various ways to sort NumPy arrays.
import numpy as np
# Create sample 1D and 2D NumPy arrays
array_1d = np.array([3, 1, 2, 5, 4])
print("1D Array:\n", array_1d)
array_2d = np.array([[3, 2, 1], [6, 5, 4], [9, 8, 7]])
print("2D Array:\n", array_2d)
# Sort the 1D array
sorted_array_1d = np.sort(array_1d)
print("Sorted 1D Array:\n", sorted_array_1d)
# Sort the 2D array along rows (axis=1)
sorted_array_2d_rows = np.sort(array_2d, axis=1)
print("2D Array Sorted Along Rows:\n", sorted_array_2d_rows)
# Sort the 2D array along columns (axis=0)
sorted_array_2d_cols = np.sort(array_2d, axis=0)
print("2D Array Sorted Along Columns:\n", sorted_array_2d_cols)
# In-place sort the 1D array
array_1d.sort()
print("In-Place Sorted 1D Array:\n", array_1d)
# Custom sorting criteria
custom_order = np.array([3, 1, 4, 2, 5])
sorted_indices = np.argsort(custom_order)
sorted_array_custom = array_1d[sorted_indices]
print("Custom Sorted 1D Array:\n", sorted_array_custom)
# Create a structured array
data = np.array([('John', 32, 75.5), ('Doe', 28, 82.1), ('Alice', 25, 65.2)],
dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])
# Sort by age
sorted_data = np.sort(data, order='age')
print("Sorted Structured Array by Age:\n", sorted_data)
Output:
1D Array:
[3 1 2 5 4]
2D Array:
[[3 2 1]
[6 5 4]
[9 8 7]]
Sorted 1D Array:
[1 2 3 4 5]
2D Array Sorted Along Rows:
[[1 2 3]
[4 5 6]
[7 8 9]]
2D Array Sorted Along Columns:
[[3 2 1]
[6 5 4]
[9 8 7]]
In-Place Sorted 1D Array:
[1 2 3 4 5]
Custom Sorted 1D Array:
[2 3 1 4 5]
Sorted Structured Array by Age:
[('Alice', 25, 65.2) ('Doe', 28, 82.1) ('John', 32, 75.5)]
Conclusion
Sorting arrays in NumPy is straightforward and efficient. Whether you’re working with simple arrays or complex structured arrays, NumPy provides versatile functions to meet your sorting needs. By leveraging these capabilities, you can efficiently process and analyze your data.