Python:NumPy .sort()
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.
Syntax of Numpy .sort()
ndarray.sort(axis=-1, kind=None, order=None)
Parameters:
axis: Axis along which to sort. Default is-1(last axis). UseNoneto 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 NumPyimport numpy as np# Create a NumPy arraynf = np.array(['aaa', 'xxx', 'ddd', 'sss'])# Sort in descending ordernf.sort() # Sorts in ascending ordernf = nf[::-1] # Reverses to get descending orderprint(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:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve 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