How to sort a Numpy Array?


The term "array" typically refers to a list. There are some similarities between NumPy arrays and Python lists, but there are also significant variations in data formats, memory utilization, performance, and functionality. In Python, we have some array in-built functions that can be used to sort a Numpy Array. The Numpy is a multi-dimensional homogeneous array of fixed-size items. The main purpose of Numpy array is used for scientific computing. For example- Numpy is commonly used in data science and machine learning program.

Syntax

The following syntax is used in the examples −

array()

This is an in-built function in Python that can be used to store the multiple elements of different data types.

sort()

This is an in-built function that can be used to represent the number or string in the order of ascending to descending.

Algorithm

The following steps are −

  • We will start the program by importing the module numpy and taking the object to represent the module.

  • Now use the in-built function array() with the object name and store it in the variable.

  • Then print the result by applying the sort() function to that variable.

Example 1

In this program, we will use some random number that stores the number in the form list. Then use of the array() function considers the number in the form of an array. Lastly, use the sort function to set the number in the order of ascending to descending number.

import numpy as pq
my_arr = pq.array( [11, 102, 1091, 2] )
print( "Sorting of Numpy array:", pq.sort(my_arr) )

Output

Sorting of Numpy array: [   2   11  102 1091]

Example 2

In this program, we will create multiple strings in the list. To convert the list into an array it will use an in-built function array() and sort the list of strings by using the sort() function.

import numpy as pq
arr = pq.array( ['School', 'Friend', 'University', 'Teacher'] )
print( "Sorting of Numpy array:", pq.sort(arr) )

Output

Sorting of Numpy array: ['Friend' 'School' 'Teacher' 'University']

Example 3

In this program, we will sort the boolean array by using an in-built function named array() and sort().

import numpy as np
np_arr = np.array( [True, False, True, True, False] )
print( "Sorting of Numpy array:", np.sort( np_arr ) )

Output

Sorting of Numpy array: [False False  True  True  True]

Example 4

In this program, we will first mention the module of Numpy. Then store the list of array using the built-in function array(). Then initialize the variable sort_index that stores the sorting of an index. Next, use the array list to store the sort_index and get the result using print function.

import numpy as np
arr = np.array([3, 2, 1])
sort_index = np.argsort(arr)
sort_arr = arr[sort_index]
print(sort_arr)

Output

[1 2 3]

Conclusion

The above three outputs represent the sorting of the Numpy array. The first example used the random integer to sort a Numpy array whereas the second example used the string to sort a Numpy array. Finally, the third example used the boolean array to sort out the Numpy array.

Updated on: 01-Jun-2023

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements