In this article we will discuss different ways to sort a numpy array in python.

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

#### numpy.ndarray.sort()

A member function of ndarray class,

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

It can be called through a numpy array object (ndarray) and it sorts the associated numpy array in place.

#### numpy.sort()

Another one is a global function in numpy module i.e.

**Advertisements**

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

It accepts a numpy array as an argument and returns a sorted copy of Numpy array.

Important Arguments in both the above functions are,

- axis: Axis along which it needs to sort. Default value is -1 i.e. the last axis
- kind: Type of sorting algorithm to be used. Values can be ‘mergesort’, ‘heapsort’, ‘stable’ and ‘quicksort’
- order: Udes in Structured Numpy array. Can be a single column name or list of column names along which sorting needs to be done.

Let’s use them to sort a numpy array.

## Sort a Numpy array in Place

First of all import numpy module i.e.

import numpy as np

Now suppose we have a numpy array,

# Create a Numpy array from list of numbers arr = np.array([6, 1, 4, 2, 18, 9, 3, 4, 2, 8, 11])

To sort this numpy array in place let’s call its member function sort() i.e.

# Sort the numpy array inplace arr.sort() print('Sorted Array : ', arr)

Output:

Sorted Array : [ 1 2 2 3 4 4 6 8 9 11 18]

It sorted the numpy array in place.

## Get a sorted copy a Numpy array

Suppose we have a numpy array,

arr = np.array([6, 1, 4, 2, 18, 9, 3, 4, 2, 8, 11])

To get the sorted copy of above created numpy array using global **numpy.sort()** function i.e.

# To Get a sorted copy of numpy array (Ascending Order) sortedArr = np.sort(arr) print('Sorted Array : ', sortedArr) print('Original Array : ', arr)

Output:

Sorted Array : [ 1 2 2 3 4 4 6 8 9 11 18] Original Array : [ 6 1 4 2 18 9 3 4 2 8 11]

It returned a sorted copy of numpy array, but the original numpy remains as it is.

Till now we have seen that by default both numpy.sort() and ndarray.sort() sorts the numpy array in ascending order. But what if we want to sort our numpy array in descending order ?

## Sort a Numpy array in Descending Order

Well there is no option or argument in both the sort() functions to change the sorting order to decreasing order. So, to sort a numpy array in descending order we need to sort it and then use [::-1] to reverse the sorted array. It will give the effect of sorting in descending order i.e.

arr = np.array([6, 1, 4, 2, 18, 9, 3, 4, 2, 8, 11]) # Get a sorted copy of numpy array (Descending Order) arr = np.sort(arr)[::-1] print('Sorted Array in Descending Order: ', arr)

Output:

Sorted Array in Descending Order: [18 11 9 8 6 4 4 3 2 2 1]

It will sort the numpy array in descending order.

## Sorting a numpy array with different kind of sorting algorithms.

Both the sort() functions accepts a parameter **‘kind’** that tells about the sorting algorithm to be used while sorting. If not provided then default value is **‘quicksort’**. To sort numpy array with other sorting algorithm pass this ‘kind’ argument. For example,

Sort numpy array using ‘**mergesort**‘ algorithm,

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

Sort numpy array using ‘**heapsort**‘ algorithm,

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

Sort numpy array using ‘**stable**‘ algorithm,

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

## Sorting a 2D numpy array along the axis

Both the sort function i.e. numpy.sort() and numpy.ndarray.sort() provides an argument axis to sort the elements along the axis. Let’s understand that by examples,

Let’s create a 2D Numpy Array i.e.

# Create a 2D Numpy array list of list arr2D = np.array([[8, 7, 1, 2], [3, 2, 3, 1], [29, 32, 11, 9]])

Contents of the created 2D numpy array,

[[ 8 7 1 2] [ 3 2 3 1] [29 32 11 9]]

#### Sort Contents of each column in 2D numpy Array

Now to sort the contents of each column in this 2D numpy array pass the axis as 0 i.e.

# Sort along axis 0 i.e. sort contents of each Column in numpy array arr2D.sort(axis=0) print('Sorted Array : ') print(arr2D)

Output:

Sorted Array : [[ 3 2 1 1] [ 8 7 3 2] [29 32 11 9]]

It basically sorted the contents of each column in ascending order in the above created numpy array.

#### Sort Contents of each Row in 2D numpy Array

Now to sort the contents of each row in this 2D numpy array pass the axis as 1 i.e.

# Sort along axis 1 i.e. sort contents of each Row in numpy array arr2D.sort(axis=1) print('Sorted Array : ') print(arr2D)

Output:

Sorted Array : [[ 1 1 2 3] [ 2 3 7 8] [ 9 11 29 32]]

It basically sorted the contents of each row in ascending order in the above created numpy array.

If we don’t provide any value for ‘axis’ parameter then its default value is -1 i.e. it sort along the last axis which in 2D array is 1. So, for 2D array axis=1 is equivalent to axis=-1 or not providing axis parameter.

**Complete example is as follows,**

import numpy as np def main(): # Create a Numpy array from list of numbers arr = np.array([6, 1, 4, 2, 18, 9, 3, 4, 2, 8, 11]) print('Original Array : ', arr) print('*** Sort a Numpy Array inplace ***') # Sort the numpy array inplace arr.sort() print('Sorted Array : ', arr) print('*** Get a Sorted copy of a Numpy Array ***') arr = np.array([6, 1, 4, 2, 18, 9, 3, 4, 2, 8, 11]) # To Get a sorted copy of numpy array (Ascending Order) sortedArr = np.sort(arr) print('Sorted Array : ', sortedArr) print('Original Array : ', arr) print('*** Sort a Numpy Array in Descending Order ***') arr = np.array([6, 1, 4, 2, 18, 9, 3, 4, 2, 8, 11]) # Get a sorted copy of numpy array (Descending Order) arr = np.sort(arr)[::-1] print('Sorted Array in Descending Order: ', arr) # Sort Using 'mergesort' sortedArr = np.sort(arr, kind='mergesort') print('Sorted Array : ', sortedArr) # Sort Using 'heapsort' sortedArr = np.sort(arr, kind='heapsort') print('Sorted Array : ', sortedArr) print('*** Sorting 2D numpy array along axis ***') # Create a 2D Numpy array list of list arr2D = np.array([[8, 7, 1, 2], [3, 2, 3, 1], [29, 32, 11, 9]]) print('2D Numpy Array') print(arr2D) print('*** Sort Contents of each Column in 2D numpy Array | Sorting along axis 0 ***') # Sort along axis 0 i.e. sort contents of each Column in numpy array arr2D.sort(axis=0) print('Sorted Array : ') print(arr2D) print('*** Sort Contents of each Row in 2D numpy Array | Sorting along axis 1 ***') # Sort along axis 1 i.e. sort contents of each Row in numpy array arr2D.sort(axis=1) print('Sorted Array : ') print(arr2D) if __name__ == '__main__': main()

**Output:**

Original Array : [ 6 1 4 2 18 9 3 4 2 8 11] *** Sort a Numpy Array inplace *** Sorted Array : [ 1 2 2 3 4 4 6 8 9 11 18] *** Get a Sorted copy of a Numpy Array *** Sorted Array : [ 1 2 2 3 4 4 6 8 9 11 18] Original Array : [ 6 1 4 2 18 9 3 4 2 8 11] *** Sort a Numpy Array in Descending Order *** Sorted Array in Descending Order: [18 11 9 8 6 4 4 3 2 2 1] Sorted Array : [ 1 2 2 3 4 4 6 8 9 11 18] Sorted Array : [ 1 2 2 3 4 4 6 8 9 11 18] *** Sorting 2D numpy array along axis *** 2D Numpy Array [[ 8 7 1 2] [ 3 2 3 1] [29 32 11 9]] *** Sort Contents of each Column in 2D numpy Array | Sorting along axis 0 *** Sorted Array : [[ 3 2 1 1] [ 8 7 3 2] [29 32 11 9]] *** Sort Contents of each Row in 2D numpy Array | Sorting along axis 1 *** Sorted Array : [[ 1 1 2 3] [ 2 3 7 8] [ 9 11 29 32]]

**Advertisements**

Thanks for reading.