Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.sort

Returns a sorted copy of an array. It can sort along an axis, flatten before sorting, choose an algorithm, preserve equal-value order, and optionally sort descending.

How it works

np.sort returns a sorted copy of array-like input. By default it sorts along the last axis using quicksort; choose axis=None to flatten first, select a sorting algorithm with kind, control structured-array field priority with order, and request stable or descending ordering with the corresponding keyword options.

Syntax

numpy.sort(a, axis=-1, kind=None, order=None, *, stable=None, descending=<no value>)

Parameters

NameTypeDescription
aarray_likeArray to be sorted.
axisint or None, optionalAxis along which to sort; None flattens the array before sorting. Default: -1.
kind{'quicksort', 'mergesort', 'heapsort', 'stable'}, optionalSorting algorithm. Options are 'quicksort', 'mergesort', 'heapsort', and 'stable'; default: 'quicksort'.
orderstr or list of str, optionalFor arrays with fields, names the fields to compare in priority order.
stablebool, optionalWhether equal values retain relative order; internally selects kind='stable'.
descendingbool, optionalWhether to sort descending; False or None sorts ascending.

Returns

sorted_array — ndarray: Array of the same type and shape as a.

Key points

  • The function leaves its input unchanged and produces a sorted copy.
  • Without axis, each one-dimensional slice along the final dimension is sorted.
  • When kind is omitted, NumPy uses quicksort.
  • For structured arrays, order establishes the priority sequence for named comparison fields.
  • stable=True preserves the input order of elements that compare equally.
  • descending=True requests descending rather than ascending output.

Common mistakes

  • Using axis=None changes the result to a one-dimensional flattened sort rather than sorting along an existing axis.
  • stable=False or stable=None does not guarantee that equal values retain their original relative order.
  • NaN values go to the end whether descending is enabled or not.

Example: Sort rows without modifying the input

import numpy as np

values = np.array([[3, 1, 2], [6, 4, 5]])
print(np.sort(values, axis=1))
# [[1 2 3]
#  [4 5 6]]

Sort each row while returning a separate array; the input remains unchanged.

When to use it

Non-mutating sorting

You need sorted values while preserving the original ndarray for later computations.

Axis-specific sorting

You need each row, column, or another dimension sorted independently.

Stable or field-priority sorting

You need deterministic ordering for records with equal comparison keys.

Interview questions

QWhat does np.sort return, and what axis does it use by default?

np.sort creates and returns a sorted copy, so the original array is not modified. With the default axis=-1, sorting occurs along the last axis.

Follow-up: How would you sort every element globally? Use axis=None, which flattens the array before sorting.

QHow do kind and stable affect sorting?

kind selects the sorting algorithm from quicksort, mergesort, heapsort, or stable. The documented default is quicksort.

Follow-up: How can you preserve the order of equal values? Set stable=True, which internally selects kind='stable'.

QWhat is order used for, and what shape does np.sort return?

For structured arrays, order specifies which named fields are compared first, second, and so on. The result remains an ndarray with the same type and shape as the input.

Follow-up: How can you sort in descending order in the current stable API? Pass descending=True; NaNs still sort to the end.

  • numpy.argsort
  • numpy.ndarray.sort
  • numpy.lexsort
  • numpy.searchsorted

Source: numpy.sort. Checked 2026-10-15.