Close this search box.

Introduction to Python NumPy Sorting Array

In This Article You Will Learn About Python Numpy Sorting Array

Python Numpy Sorting – Before moving ahead, let’s know a little bit about Python Numpy Searching Array

Sorting is the process of arranging elements in specified order.

An ordered sequence is process to specify in which order, elements to be specified such as alphabetical, numeric, ascending, or descending.

A function called sort() comes in the NumPy to sort (arrange sequence) a given array.

Example – Sorting elements of the array (arranging sequence).

import numpy as np
Array = np.array([5, 2, 4, 1, 3])

Output - 

[1, 2, 3, 4, 5] 

As shown above, it returned sorted array (arranged elements).

Sort function allows to sort the arrays in different data types such as string or other types.

Example – Sorting elements of the array alphabetically.

import numpy as np
Array = np.array(['Key', 'Chain', 'Lock'])

Output -

['Chain' 'Key' 'Lock']

As shown above, it returned sorted array (arranged elements) on based of alphabet.

Example – Sorting the elements of a Boolean array.

import numpy as np
Array = np.array([True, False])

Output - 

[False  True]

As shown above, it returned sorted array (arranged elements) based on Boolean value.

Sorting a 2-D Array

If we apply sort function in 2-D Arrays, then it applies (sort) on both Arrays.

Example – Sorting elements of 2-D Array.

import numpy as np
Array = np.array([[13, 12, 14], [15, 10, 11]]) 

Ouput -

[[12, 13, 14] 
   [10, 11, 15]]

Henceforth, it returned 2-D sorted array )arranged elements.

NumPy Filtering Array

Filtering is the process of taking out elements from the original Array and create a new one.

A list of Boolean index is available to filter an NumPy Array.

If the value of the Index number belongs to True, then element of that index number will be filtered otherwise value of the index number False, will not include element to filter an Array.

Example – Create an Array from the elements on based index Boolean value.

import numpy as np

Array = np.array([11, 12, 13, 14])
x = [True, False, True, False]
updated_Array = Array[x]


As shown above, it returned a filter Array containing two elements i.e., [11, 13]. It is because filter Array included only those elements which are placed at in place of True (Boolean value).

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

Recent Post

Popular Post

Top Articles


Share on