Numpy.sort() in python

The np.sort() function is used to sort the array along a specified axis.

Numpy.sort (a, axis=- 1, kind=None, order=None)

  • Purpose: This function is used for sorting the array.
  • Parameters:
    • arr:a:array_like array to be sorted.
    • axis: None or int,optional Axis on which we perform the arithmetic mean if specified. otherwise, the arr will be flattened.
    • kind: {‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, optional Kind of sorting algorithm that should be used on the array if required.
    • order: str or list of str, optional which fields should be compared first.
  • Returns:
    • sorted array:ndarray_ Returns array of same type and shape as a
PyInteractive Python
# Import Packages import numpy as np

  

Numpy Sort Function

The numpy.sort() function takes in the array as one of the required arguments and returns a copy of the sorted array. Let’s take an example to understand this.

1. Sort 1-D array

Let’s see, how to sort a 1-D numpy array.

Example 1: Sort a 1-D numpy array with default parameters.

PyInteractive Python
# Create a 1-D array a = np.array([3,6,5,2,1])

  
PyInteractive Python
# Apply sort method on the above array sort_a = np.sort(a)

  
PyInteractive Python
# Print the sorted array print(sort_a) #> [1 2 3 5 6]

  

The above array is sorted in ascending order. because you have not mentioned any parameters.

2. Sort on 2-D array

Let’s see, sort of a 2-D numpy array using default parameters.

Example 1: Sort a 2-D numpy array with default parameters.

PyInteractive Python
# Create a 2-D array a = np.array([[2,5,4],[7,4,9]])

  
PyInteractive Python
# Apply sort method on the above array sort_a = np.sort(a)

  
PyInteractive Python
# print sorted array print(sort_a) #> [[2 4 5] #> [4 7 9]]

  

The array is sorted in ascending order along axis 0 as axis 0 is the default parameter.

Example 2: Sort a 2-D numpy array with axis = 0

PyInteractive Python
# Create a 2-D array a = np.array([[2,5,4,0],[7,4,9,1]])

  
PyInteractive Python
# Apply sort method on the above array sort_a = np.sort(a, kind = 'quicksort', axis = 0)

  
PyInteractive Python
# print sorted array print(sort_a) #> [[2 4 4 0] #> [7 5 9 1]]

  

array = [[2,5,4,0],[7,4,9,1]]

sub array1 = [2,5,4,0]
sub array2 = [7,4,9,1]

  • The sort() method in this example compares the sub array1 1st column value with sub array2 1st column value.
  • Which among the compared values are small that will be sorted first.
  • The same repeats with all column values in sub arrays.

Example 3: Sort a 2-D numpy array with axis = 1

PyInteractive Python
# Create a 2-D array a = np.array([[2,5,4,0],[7,4,9,1]])

  
PyInteractive Python
# Apply sort method on the above array sort_a = np.sort(a, kind = 'quicksort', axis = 1)

  
PyInteractive Python
# print sorted array print(sort_a) #> [[0 2 4 5] #> [1 4 7 9]]

  

Here, The sort() method is performed on entire sub array1 and sorts the values in that sub array1 in ascending order.

The same applies to sub array2

3.Test your knowledge

Q1: Can you perform a sort operation on 1-D array when axis=1?

Ans: No, you cannot perform a sort operation on 1-D array when axis=1. It returns a error.