numpy.sort() in Python | np.sort() in Python

Numpy sort() function is used to sort Numpy array without modifying original array.

Syntax of Numpy sort()

np.sort(arr, axis=-1, kind='quicksort', order=None)

arr parameter is used to specify the array that you want to sort.

axis parameter is used to specify the axis along which you want to sort array. If the value of axis is None then the array is flattened first and then sorted next.

kind parameter is used to specify the sorting algorithm. You can provide quicksort, mergesort, heapsort or stable. Default value is quicksort.

order parameter is used to sort array based on particular fields. This parameter is useful when your array is structured array.

Python Program to sort 1-D Numpy array

import numpy as np
 
a = np.array([32, 4, 8, 12, 20])
print('Sorted array is', np.sort(a))
print('Orginal array is', a)

Output of the above program

Sorted array is [ 4  8 12 20 32]
Orginal array is [32  4  8 12 20]
Numpy sort on 1-Dimensional Array

As you can see in the above output, np.sort() has sorted all the elements of Numpy array. In 1-D array, it is optional to pass axis parameter to sort() function.

Python Program to sort 2-D Numpy array

import numpy as np
 
a = np.array([[32, 4, 8, 12, 30], [35, 5, 15, 10, 20]])
print('Sorted array when axis is None\n', np.sort(a, axis=None))
print('Sum of arr along axis=0 is\n', np.sort(a, axis=0))
print('Sum of arr along axis=1 is\n', np.sort(a, axis=1))

Output of the above program

Sorted array when axis is None
 [ 4  5  8 10 12 15 20 30 32 35]
Sum of arr along axis=0 is
 [[32  4  8 10 20]
 [35  5 15 12 30]]
Sum of arr along axis=1 is
 [[ 4  8 12 30 32]
 [ 5 10 15 20 35]]

When axis value is None, then sort() function works like this-

Numpy sort on 2-Dimensional Array with axis=None

When axis=0 is passed to sort() function, then it works like this-

Numpy sort on 2-Dimensional Array with axis=0

When axis=1 is passed to sort() function, then it works like this-

Numpy sort on 2-Dimensional Array with axis=1

Python Program to sort Numpy structured array

import numpy as np

t = np.dtype([('productname', 'U30'), 
              ('numberofquantity', 'uint8'), 
              ('price', 'float32'), 
              ('inStock', 'bool')])

arr = np.array([('Samsung Galaxy S2', 23, 15000, True), 
                ('Apple iPhone X', 56, 80000, True), 
                ('Motorola M20', 31, 11500, False),
                ('Vivo V15 Pro', 56, 26000, True)], dtype=t)
#Sorting array on the basis of productname
print(np.sort(arr, order='productname'))
#Sorting array on the basis of price first and then on productname
print(np.sort(arr, order=['price', 'productname']))

Output of the above program

[('Apple iPhone X', 56, 80000.,  True) ('Motorola M20', 31, 11500., False)
 ('Samsung Galaxy S2', 23, 15000.,  True)
 ('Vivo V15 Pro', 56, 26000.,  True)]
[('Motorola M20', 31, 11500., False)
 ('Samsung Galaxy S2', 23, 15000.,  True)
 ('Vivo V15 Pro', 56, 26000.,  True) ('Apple iPhone X', 56, 80000.,  True)]

np.sort(arr, order='productname') means array is sorted by productname

np.sort(arr, order=['price', 'productname']) means array is sorted by price and then productname if prices are equal.

Recommended Posts