sort array in python numpy code example

Example 1: numpy sort

>>> a = np.array([[1,4],[3,1]])
>>> np.sort(a)                # sort along the last axis
array([[1, 4],
       [1, 3]])
>>> np.sort(a, axis=None)     # sort the flattened array
array([1, 1, 3, 4])
>>> np.sort(a, axis=0)        # sort along the first axis
array([[1, 1],
       [3, 4]])

Example 2: sort numpy array

import numpy as np

arr = np.array([3, 2, 0, 1])

print(np.sort(arr))

Example 3: sort numpy array

import numpy as np

arr = np.array(['banana', 'cherry', 'apple'])

print(np.sort(arr))