NumPy ndarray.sort()

The numpy.ndarray.sort() method sorts the elements of a NumPy array in place along a specified axis. It supports different sorting algorithms and allows sorting by specific fields for structured arrays.

Syntax

</>
Copy
ndarray.sort(axis=-1, kind=None, order=None)

Parameters

ParameterTypeDescription
axisint, optionalAxis along which to sort. Default is -1, meaning sorting along the last axis.
kind{‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, optionalSorting algorithm to use. Default is 'quicksort'. The 'stable' and 'mergesort' options use timsort internally.
orderstr or list of str, optionalFor structured arrays, specifies which fields to sort by.

Return Value

This method sorts the array in place and does not return a new array. The sorting modifies the existing array.


Examples

1. Sorting a 1D NumPy Array

Sorting a simple one-dimensional NumPy array using the default quicksort algorithm.

</>
Copy
import numpy as np  

# Creating a 1D array
arr = np.array([7, 2, 5, 1, 9, 3])

# Sorting the array in-place
arr.sort()

# Display the sorted array
print(arr)

Output:

[1 2 3 5 7 9]

2. Sorting a 2D NumPy Array Along a Specific Axis

Sorting a two-dimensional NumPy array along different axes.

</>
Copy
import numpy as np  

# Creating a 2D array
arr = np.array([[8, 2, 5], 
                [3, 7, 1]])

# Sorting along the last axis (axis=-1, sorting each row)
arr.sort(axis=-1)
print("Sorted along last axis (rows):\n", arr)

# Sorting along the first axis (axis=0, sorting each column)
arr.sort(axis=0)
print("Sorted along first axis (columns):\n", arr)

Output:

Sorted along last axis (rows):
[[2 5 8]
 [1 3 7]]

Sorted along first axis (columns):
[[1 3 7]
 [2 5 8]]

3. Using Different Sorting Algorithms

Sorting an array using different sorting algorithms: quicksort, mergesort, and heapsort.

</>
Copy
import numpy as np  

# Creating a 1D array
arr = np.array([9, 4, 6, 2, 8])

# Sorting using quicksort (default)
arr_quick = np.array(arr)  # Creating a copy
arr_quick.sort(kind='quicksort')

# Sorting using mergesort
arr_merge = np.array(arr)  # Creating a copy
arr_merge.sort(kind='mergesort')

# Sorting using heapsort
arr_heap = np.array(arr)  # Creating a copy
arr_heap.sort(kind='heapsort')

print("Sorted using quicksort:", arr_quick)
print("Sorted using mergesort:", arr_merge)
print("Sorted using heapsort:", arr_heap)

Output:

Sorted using quicksort: [2 4 6 8 9]
Sorted using mergesort: [2 4 6 8 9]
Sorted using heapsort: [2 4 6 8 9]

4. Sorting a Structured Array Using the order Parameter

Sorting a structured NumPy array based on specific fields using the order parameter.

</>
Copy
import numpy as np  

# Creating a structured array with named fields
dtype = [('name', 'U10'), ('age', int), ('score', float)]
arr = np.array([('Arjun', 25, 89.5),
                ('Bhairav', 23, 92.0),
                ('Charlie', 27, 85.0)], dtype=dtype)

# Sorting by 'age'
arr.sort(order='age')
print("Sorted by age:\n", arr)

# Sorting by 'score'
arr.sort(order='score')
print("Sorted by score:\n", arr)

Output:

Sorted by age:
 [('Bhairav', 23, 92. ) ('Arjun', 25, 89.5) ('Charlie', 27, 85. )]
Sorted by score:
 [('Charlie', 27, 85. ) ('Arjun', 25, 89.5) ('Bhairav', 23, 92. )]

Here, the structured array is sorted based on different fields. The order parameter allows specifying which field to use for sorting.