Summary: in this tutorial, you’ll learn how to use the numpy sort()
function to sort elements of an array.
Introduction to the NumPy sort() function
The
function returns a sorted copy of an array. Here’s the syntax of the sort()
function:sort()
numpy.sort(a, axis=- 1, kind=None, order=None)
Code language: Python (python)
In this syntax:
a
is a numpy array to be sorted. Also, it can be any object that can be converted to an array.axis
specifies the axis along which the elements will be sorted. If the axis is None, the function flattens the array before sorting. By default, the axis is -1 which sorts elements along the last axis.kind
specifies the sorting algorithm which can be ‘quicksort’, ‘mergesort’, ‘heapsort’, and ‘stable’.order
specifies which fields to compare first, second, etc when sorting an array with fields defined. Theorder
can be a string that represents the field to sort or a list of strings that represent a list of fields to sort.
If you want to sort the elements of an array in place, you can use the sort()
method of the ndarray
object with the following syntax:
ndarray.sort(axis=- 1, kind=None, order=None)
Code language: Python (python)
NumPy sort() function examples
Let’s take some examples of using the NumPy sort()
function.
1) Using the sort() function to sort a 1-D array
The following example uses the sort()
function to sort numbers in a 1-D array:
import numpy as np
a = np.array([2, 3, 1])
b = np.sort(a)
print(b)
Code language: Python (python)
Output:
[1 2 3]
Code language: Python (python)
In this example, the sort()
function sorts the elements of the array from low to high.
To sort elements of an array from high to low, you can use the sort()
function to sort an array from low to high and use a slice to reverse the array. For example:
import numpy as np
a = np.array([2, 3, 1])
b = np.sort(a)[::-1]
print(b)
Code language: Python (python)
Output:
[3 2 1]
Code language: Python (python)
In this example:
- First, the
sort()
function sorts the elements in the arraya
in ascending order (from low to high) - Then, the slice [::-1] reverses the sorted array so that the elements of the result array are in descending order.
2) Using the numpy sort() function to sort a 2-D array example
The following example uses the sort()
funciton to sort a 2-D array:
import numpy as np
a = np.array([
[2, 3, 1],
[5, 6, 4]
])
b = np.sort(a)
print(b)
Code language: Python (python)
Output:
[[1 2 3]
[4 5 6]]
Code language: Python (python)
The following example uses the sort()
function to sort elements on axis 0:
import numpy as np
a = np.array([
[5, 3, 4],
[2, 6, 1]
])
b = np.sort(a, axis=0)
print(b)
Code language: Python (python)
Output:
[[2 3 1]
[5 6 4]]
Code language: Python (python)
Similarly, you can sort elements of the array on axis 1:
import numpy as np
a = np.array([
[5, 3, 4],
[2, 6, 1]
])
b = np.sort(a, axis=1)
print(b)
Code language: Python (python)
Output:
[[3 4 5]
[1 2 6]]
Code language: JSON / JSON with Comments (json)
3) Using the numpy sort() function to sort a structured array example
The following example sorts the employees by year of services and then salary:
import numpy as np
dtype = [('name', 'S10'),
('year_of_services', float),
('salary', float)]
employees = [
('Alice', 1.5, 12500),
('Bob', 1, 15500),
('Jane', 1, 11000)
]
payroll = np.array(employees, dtype=dtype)
result = np.sort(
payroll,
order=['year_of_services', 'salary']
)
print(result)
Code language: Python (python)
Output:
[(b'Jane', 1. , 11000.) (b'Bob', 1. , 15500.) (b'Alice', 1.5, 12500.)]
Code language: Python (python)
Summary
- Use the numpy
sort()
function to sort elements of an array.