NumPy Array Sorting
- Previous Page NumPy Array Search
- Next Page NumPy Array Filtering
Array Sorting
Sorting is the process of arranging elements in an ordered sequence.
An ordered sequence is any sequence that has an order corresponding to its elements, such as numbers or letters, in ascending or descending order.
The NumPy ndarray object has a named sort()
The function, which sorts the specified array.
Example
Sort Array:
import numpy as np arr = np.array([3, 2, 0, 1]) print(np.sort(arr))
Note:This method returns a copy of the array, while the original array remains unchanged.
You can also sort string arrays or any other data type:
Example
Sort Array in Alphabetical Order:
import numpy as np arr = np.array(['banana', 'cherry', 'apple']) print(np.sort(arr))
Example
Sort Boolean Array:
import numpy as np arr = np.array([True, False, True]) print(np.sort(arr))
Sort 2-D Array
If the sort() method is used on a two-dimensional array, it will sort two arrays:
Example
Sort 2-D Array
import numpy as np arr = np.array([[3, 2, 4], [5, 0, 1]]) print(np.sort(arr))
- Previous Page NumPy Array Search
- Next Page NumPy Array Filtering