LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.
Python Data Science NumPy Sorting Arrays
Sorting Arrays
Putting elements in an ordered sequence means Sorting.
Ordered sequence is any sequence that has an order corresponding to elements, like numeric or alphabetical, ascending or descending.
The NumPy ndarray object has a function called sort(), that will sort a specified array.
Example 1: Sort the array
code
import numpy as np
arr = np.array([13, 12, 10, 11])
print(np.sort(arr))
the output will be
[10 11 12 13]
You can also sort arrays of strings, or any other data type.
Example 2: Sort the array alphabetically.
code
import numpy as np
arr = np.array(['orange', 'grapes', 'apple'])
print(np.sort(arr))
the output will be
['apple' 'grapes' 'orange']
Example 3: Sort a boolean array.
Code
import numpy as np
arr = np.array([True, False, True])
print(np.sort(arr))
the output will be
[False True True]
Sorting a 2-D Array
If you use the sort() method on a 2-D array, both arrays will be sorted.
Example 4: Sort a 2-D array.
Code
import numpy as np
arr = np.array([[3, 2, 4], [5, 0, 1]])
print(np.sort(arr))
the output will be
[[2 3 4] [0 1 5]]