numpy.sort in Python

In some cases, we require a sorted array for computation. For this purpose, the numpy module of Python provides a function called numpy.sort(). This function gives a sorted copy of the source array or input array.

numpy-sort

Syntax:

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

Parameters:

x: array_like

This parameter defines the source array, which is going to be sorted.

axis: int or None(optional)

This parameter defines the axis along which the sorting is performed. If this parameter is None, the array will be flattened before sorting, and by default, this parameter is set to -1, which sorts the array along the last axis.

kind: {quicksort, heapsort, mergesort}(optional)

This parameter is used to define the sorting algorithm, and by default, the sorting is performed using 'quicksort'.

order: str or list of str(optional)

When an array is defined with fields, its order defines the fields for making a comparison in first, second, etc. Only the single field can be specified as a string, and not necessarily for all fields. However, the unspecified fields will still be used, in the order in which they come up in the dtype, to break the ties.

Returns:

This function returns a sorted copy of the source array, which will have the same shape and type as a source array.

Example 1:

snippet
import numpy as np
x=np.array([[1,4,2,3],[9,13,61,1],[43,24,88,22]])
x
y=np.sort(x)
y

Output:

Output
array([[ 1, 4, 2, 3], [ 9, 13, 61, 1], [43, 24, 88, 22]]) array([[ 1, 2, 3, 4], [ 1, 9, 13, 61], [22, 24, 43, 88]])

In the above code

  • We have imported numpy with alias name np.
  • We have created a multi-dimensional array 'x' using np.array() function.
  • We have declared the variable 'y' and assigned the returned value of np.sort() function.
  • We have passed the input array 'x' in the function.
  • Lastly, we tried to print the value of 'y'.

In the output, it shows a sorted copy of the source array of the same type and shape.

Example 2:

snippet
import numpy as np
x=np.array([[1,4,2,3],[9,13,61,1],[43,24,88,22]])
x
y=np.sort(x, axis=None)
y

Output:

Output
array([[ 1, 4, 2, 3], [ 9, 13, 61, 1], [43, 24, 88, 22]]) array([ 1, 1, 2, 3, 4, 9, 13, 22, 24, 43, 61, 88])

Example 3:

snippet
import numpy as np
x=np.array([[1,4,2,3],[9,13,61,1],[43,24,88,22]])
x
y=np.sort(x,axis=0)
y
z=np.sort(x,axis=1)
z

Output:

Output
array([[ 1, 4, 2, 1], [ 9, 13, 61, 3], [43, 24, 88, 22]]) array([[ 1, 2, 3, 4], [ 1, 9, 13, 61], [22, 24, 43, 88]])

Example 4:

snippet
import numpy as np
dtype = [('name', 'S10'), ('height', float), ('age', int),('gender','S10')]
values = [('Shubham', 5.9, 23, 'M'), ('Arpita', 5.6, 23, 'F'),('Vaishali', 5.2, 30, 'F')]
x=np.array(values, dtype=dtype)
x
y=np.sort(x, order='age')
y
z=np.sort(x, order=['age','height'])
z

Output:

Output
array([('Shubham', 5.9, 23, 'M'), ('Arpita', 5.6, 23, 'F'), ('Vaishali', 5.2, 30, 'F')],dtype=[('name', 'S10'), ('height', '<f8'), ('age', '<i4'), ('gender', 'S10')]) array([('Arpita', 5.6, 23, 'F'), ('Shubham', 5.9, 23, 'M'), ('Vaishali', 5.2, 30, 'F')], dtype=[('name', 'S10'), ('height', '<f8'), ('age', '<i4'), ('gender', 'S10')]) array([('Arpita', 5.6, 23, 'F'), ('Shubham', 5.9, 23, 'M'), ('Vaishali', 5.2, 30, 'F')], dtype=[('name', 'S10'), ('height', '<f8'), ('age', '<i4'), ('gender', 'S10')])

In the above code

  • We have imported numpy with alias name np.
  • We have defined the fields and values for the structured array.
  • We have created a structured array 'x' by passing dtype and values in the np.array() function.
  • We have declared the variables 'y' and 'z', and assigned the returned value of np.sort() function.
  • We have passed the input array 'x' and order in the function.
  • Lastly, we tried to print the value of 'y' and 'z'.

In the output, it shows a sorted copy of the structured array with a defined order.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +