How to sort an array in Python

You can sort the NumPy array with the following code. If you want to learn Python, I highly recommend reading This Book.

How to sort an array in Python
How to sort an array in Python

Example 1

import numpy as np

# Original Array
a = np.array([10, 5, 20, 1])

# Sorted Array
b = np.sort(a)

# Display sorted Array
print(b)

Output

[ 1  5 10 20]

Example 2

import numpy as np

# Original Array
a = np.array([[10, 5, 20, 1],[15, 5, 30, 25]])

# Sorted Array
b = np.sort(a)

# Display sorted Array
print(b)

Output

[[ 1  5 10 20]
 [ 5 15 25 30]]

People are also reading:

Best Python Books

What is Computer Vision? Examples, Applications, Techniques

Books for Machine Learning (ML)

Free Learning Resources

Leave a Comment

Your email address will not be published.