Python:NumPy .sort()

Anonymous contributor's avatar
Anonymous contributor
Published Oct 30, 2025
Contribute to Docs

The .sort() method sorts a NumPy array in-place in ascending order along a specified axis. It modifies the original array and does not return a new one.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax of Numpy .sort()

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

Parameters:

  • axis: Axis along which to sort. Default is -1 (last axis). Use None to sort the flattened array.
  • kind: Sorting algorithm: 'quicksort' (default), 'mergesort', 'heapsort', or 'stable'.
  • order: Only for structured arrays. Specifies the field(s) to sort by.

Example

In this example, the .sort() method is used to sort a NumPy array in descending order:

# Import NumPy
import numpy as np
# Create a NumPy array
nf = np.array(['aaa', 'xxx', 'ddd', 'sss'])
# Sort in descending order
nf.sort() # Sorts in ascending order
nf = nf[::-1] # Reverses to get descending order
print(nf)

The above code will result in the following output:

['xxx' 'sss' 'ddd' 'aaa']

Codebyte Example

In the following codebyte example, a NumPy array nf is created with integers, sorted in ascending order using NumPy’s .sort() method, and then reversed to display the values in descending order:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours