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,

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.

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.

Now suppose we have a numpy array,

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

Output:

It sorted the numpy array in place.

Get a sorted copy a Numpy array

Suppose we have a numpy array,

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

Output:

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.

Output:

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 numpy array using ‘heapsort‘ algorithm,

Sort numpy array using ‘stable‘ algorithm,

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.

Contents of the created 2D numpy array,

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.

Output:

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.

Output:

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,

Output:

 

Join LinkedIn Group of Python Professional Developers who wish to expand their network and share ideas.

You can also follow us On Twitter :

Click Here to Subscribe for more Articles / Tutorials like this.