How to sort a Numpy Array in Python

How to sort a Numpy Array in Python? | numpy.sort() in Python | Python numpy.ndarray.sort() Function

In this article, we are going to show you sorting a NumpyArray in descending and in ascending order or sorts the elements from largest to smallest value or smallest to largest value. Stay tuned to this page and collect plenty of information about the numpy.sort() in Python and How to sort a Numpy Array in Python?

Sorting Arrays

Giving arrays to an ordered sequence is called sorting. An ordered sequence can be any sequence like numeric or alphabetical, ascending or descending.

Python’s Numpy module provides two different methods to sort a numpy array.

numpy.ndarray.sort() method in Python

A member function of the ndarray class is as follows:

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

This can be ordered through a numpy array object (ndarray) and it classifies the incorporated numpy array in place.

Do Check:

Python numpy.sort() method

One more method is a global function in the numpy module i.e.

numpy.sort(array, axis=-1, kind='quicksort', order=None)

It allows a numpy array as an argument and results in a sorted copy of the Numpy array.

Where,

Sr.No. Parameter & Description
1 a

Array to be sorted

2 axis

The axis along which the array is to be sorted. If none, the array is flattened, sorting on the last axis

3 kind

Default is quicksort

4 order

If the array contains fields, the order of fields to be sorted

Sort a Numpy Array in Place

The NumPy ndarray object has a function called sort() that we will use for sorting.

import numpy as np
# Create a Numpy array from list of numbers
arr = np.array([9, 1, 3, 2, 17, 5,  2, 8, 14])
array = np.array([9, 1, 3, 2, 17, 5,  2, 8, 14])
# Sort the numpy array inplace
array.sort()
print('Original Array : ', arr)
print('Sorted Array : ', array)

Output:

RESTART: C:/Users/HP/Desktop/article3.py
Original Array : [ 9 1 3 2 17 5 2 8 14]
Sorted Array : [ 1 2 2 3 5 8 9 14 17]

So in the above example, you have seen that usingarray.sort()we have sorted our array in place.

Sort a Numpy array in Descending Order

In the above method, we have seen that by default both numpy.sort() and ndarray.sort() sorts the numpy array in ascending order. But now, you will observe how to sort an array in descending order?

import numpy as np
# Create a Numpy array from list of numbers
arr = np.array([9, 1, 3, 2, 17, 5,  2, 8, 14])
array = np.array([9, 1, 3, 2, 17, 5,  2, 8, 14])
# Sort the numpy array inplace
array = np.sort(arr)[::-1]

# Get a sorted copy of numpy array (Descending Order)
print('Original Array : ', arr)
print('Sorted Array : ', array)

Output:

RESTART: C:/Users/HP/Desktop/article3.py
Original Array : [ 9 1 3 2 17 5 2 8 14]
Sorted Array : [17 14 9 8 5 3 2 2 1]

Sorting a numpy array with different kinds of sorting algorithms

While sorting sort() function accepts a parameter ‘kind’ that tells about the sorting algorithm to be used. If not provided then the default value is ‘quicksort’. To sort numpy array with another sorting algorithm pass this ‘kind’ argument.

import numpy as np
# Create a Numpy array from list of numbers
arr = np.array([9, 1, 3, 2, 17, 5,  2, 8, 14])
array = np.array([9, 1, 3, 2, 17, 5,  2, 8, 14])
# Sort the numpy array using different algorithms

#Sort Using 'mergesort'
sortedArr1 = np.sort(arr, kind='mergesort')

# Sort Using 'heapsort'
sortedArr2 = np.sort(arr, kind='heapsort')

# Sort Using 'heapsort'
sortedArr3 = np.sort(arr, kind='stable')

# Get a sorted copy of numpy array (Descending Order)
print('Original Array : ', arr)

print('Sorted Array using mergesort: ', sortedArr1)

print('Sorted Array using heapsort : ', sortedArr2)

print('Sorted Array using stable : ', sortedArr3)

Output:

RESTART: C:/Users/HP/Desktop/article3.py
Original Array : [ 9 1 3 2 17 5 2 8 14]
Sorted Array using mergesort: [ 1 2 2 3 5 8 9 14 17]
Sorted Array using heapsort : [ 1 2 2 3 5 8 9 14 17]
Sorted Array using stable : [ 1 2 2 3 5 8 9 14 17]

Sorting a 2D numpy array along with the axis

numpy.sort() and numpy.ndarray.sort() provides an argument axis to sort the elements along the axis.
Let’s create a 2D Numpy Array.

import numpy as np
# Create a 2D Numpy array list of list

arr2D = np.array([[8, 7, 1, 2], [3, 2, 3, 1], [29, 32, 11, 9]])

print(arr2D)

Output:

RESTART: C:/Users/HP/Desktop/article3.py
[[ 8 7 1 2]
[ 3 2 3 1]
[29 32 11 9]]

Now sort contents of each column in 2D numpy Array,

import numpy as np
# Create a 2D Numpy array list of list

arr2D = np.array([[8, 7, 1, 2], [3, 2, 3, 1], [29, 32, 11, 9]])
arr2D.sort(axis=0)
print('Sorted Array : ')
print(arr2D)

Output:

RESTART: C:/Users/HP/Desktop/article3.py
Sorted Array :
[[ 3 2 1 1]
[ 8 7 3 2]
[29 32 11 9]]

So here is our sorted 2D numpy array.