Python - numpy.sort() Function



In Python, numpy.sort() function is used to sort the elements of a NumPy array along a specified axis. This function sorts the array in ascending order by default, but can also sort in descending order by modifying the array or using other approaches.

It works with multi-dimensional arrays and provides flexibility in sorting either entire arrays or individual rows/columns.

Syntax

Following is the syntax of the Python numpy.sort() function −

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

Parameters

Following are the parameters of the Python numpy.sort() function −

  • a: The input array to be sorted.
  • order (optional): Specifies the field order for structured arrays. For example, sorting by multiple fields in a structured array.
  • stable(optional): This is sorting stability. If True, the returned array will maintain the relative order of arr values which compare as equal. If False or None, this is not guaranteed. Internally, this option selects kind='stable'. Default value is None.
  • axis (optional): The axis along which to sort −
    • -1 (default): Sorts along the last axis.
    • 0: Sorts along columns (vertically).
    • 1: Sorts along rows (horizontally).
  • kind (optional): Specifies the sorting algorithm. Options include −
  • quicksort (default): Fast but not stable.
  • mergesort: Stable and maintains the relative order of equal elements.
  • heapsort: Memory-efficient but slower than 'quicksort'.

Return Type

This function returns a sorted array, which is a new array if the input is not modified in place.

Example

Following is a basic example to sort a 1-dimensional NumPy array using the Python numpy.sort() function −

import numpy as np
my_array = np.array([25, 5, 15, 10])
sorted_array = np.sort(my_array)
print("Original Array:", my_array)
print("Sorted Array:", sorted_array)

Output

Following is the output of the above code −

Original Array: [25  5 15 10]
Sorted Array: [ 5 10 15 25]

Example: Sorting Along Axis

We can sort along a specific axis in a multi-dimensional array using the axis parameter. The following example demonstrates sorting rows (axis=1) and columns (axis=0) −

import numpy as np
my_array = np.array([[8, 2, 7], [4, 9, 1]])
sorted_row = np.sort(my_array, axis=1)
sorted_col = np.sort(my_array, axis=0)
print("Original Array:\n", my_array)
print("Sorted by Rows:\n", sorted_row)
print("Sorted by Columns:\n", sorted_col)

Output

Following is the output of the above code −

Original Array:
 [[8 2 7]
  [4 9 1]]
Sorted by Rows:
 [[2 7 8]
  [1 4 9]]
Sorted by Columns:
 [[4 2 1]
  [8 9 7]]

Example: Sorting Using Custom Data Type

The order parameter is used for structured arrays. Here's an example to sort by specific fields −

import numpy as np
my_array = np.array([(1, 'Zebra', 100), (2, 'Apple', 50), (3, 'Monkey', 75)],
                    dtype=[('id', int), ('name', 'U10'), ('score', int)])
sorted_by_name = np.sort(my_array, order='name')
print("Original Array:\n", my_array)
print("Sorted by Name:\n", sorted_by_name)

Output

Following is the output of the above code −

Original Array:
 [(1, 'Zebra', 100) (2, 'Apple',  50) (3, 'Monkey',  75)]
Sorted by Name:
 [(2, 'Apple',  50) (3, 'Monkey',  75) (1, 'Zebra', 100)]
numpy_array_manipulation.htm
Advertisements