🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.sort()

AI & DATA SCIENCE // np-sort

np.sort() returns a new, sorted copy of an array, leaving the original array unmodified — the array method arr.sort() sorts in place instead.

Syntax

np.sort(arr, axis=-1, kind=None)

Deep Dive Course

np.sort() always returns a brand-new sorted array, while calling .sort() as a method directly on an array sorts it in place and returns None — the same distinction as Python's sorted() versus list.sort(). For a multi-dimensional array, np.sort() sorts along the last axis by default, sorting each row independently rather than sorting the whole array as one flattened sequence; pass axis=None to sort a fully flattened copy instead.

1Understanding np.sort()

np.sort() always returns a brand-new sorted array, while calling .sort() as a method directly on an array sorts it in place and returns None — the same distinction as Python's sorted() versus list.sort(). For a multi-dimensional array, np.sort() sorts along the last axis by default, sorting each row independently rather than sorting the whole array as one flattened sequence; pass axis=None to sort a fully flattened copy instead.

💡

np.sort(), which returns a new array, and arr.sort(), which sorts in place and returns None, are easy to confuse — the same distinction as Python's sorted() versus list.sort() — pick np.sort() when you need to keep the original array's order intact.

editor.html
import numpy as np

arr = np.array([3, 1, 4, 1, 5, 9])
sorted_arr = np.sort(arr)
print(sorted_arr)
print(arr)
localhost:3000

2Practical Example

Here is a real-world application of np.sort() showing how it is used in production NumPy code.

editor.html
import numpy as np

matrix = np.array([[3, 1, 2], [6, 5, 4]])
print(np.sort(matrix, axis=1))
localhost:3000

3Best Practices

Follow these guidelines when working with np.sort():

1. Use np.sort() when the original array's order needs to be preserved; use arr.sort() only when in-place mutation is actually intended

2. Specify axis explicitly for multi-dimensional arrays, since the default sorts each row independently along the last axis, not the whole flattened array

3. Use np.argsort() instead of np.sort() when you need the sorted order as indices to apply to other related arrays, not just the sorted values themselves

⚠️

Tip: np.sort(), which returns a new array, and arr.sort(), which sorts in place and returns None, are easy to confuse — the same distinction as Python's sorted() versus list.sort() — pick np.sort() when you need to keep the original array's order intact.

editor.html
import numpy as np

arr = np.array([3, 1, 4, 1, 5, 9])
sorted_arr = np.sort(arr)
print(sorted_arr)
print(arr)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([3, 1, 4, 1, 5, 9])
sorted_arr = np.sort(arr)
print(sorted_arr)
print(arr)
Example 02Advanced Example
import numpy as np

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

Best Practices

  • Use np.sort() when the original array's order needs to be preserved; use arr.sort() only when in-place mutation is actually intended
  • Specify axis explicitly for multi-dimensional arrays, since the default sorts each row independently along the last axis, not the whole flattened array
  • Use np.argsort() instead of np.sort() when you need the sorted order as indices to apply to other related arrays, not just the sorted values themselves

Interview Question

Why does printing arr after calling np.sort(arr) still show the original, unsorted order?

Hint: Think about the difference between np.sort() and the in-place .sort() method.

np.sort(arr) computes and returns a brand-new sorted array without modifying arr at all — it's a pure function in that sense. To sort an array in place, you'd instead call the array's own .sort() method directly, which mutates arr and returns None. Since np.sort() leaves the original array completely untouched, you need to capture its return value in a new variable to actually use the sorted result.

Exercises

MediumPractice using np.sort() in a real scenario.
View Solution
import numpy as np

arr = np.array([3, 1, 4, 1, 5, 9])
sorted_arr = np.sort(arr)
print(sorted_arr)
print(arr)

Frequently Asked Questions

Why does printing arr after calling np.sort(arr) still show the original, unsorted order?

np.sort(arr) computes and returns a brand-new sorted array without modifying arr at all — it's a pure function in that sense. To sort an array in place, you'd instead call the array's own .sort() method directly, which mutates arr and returns None. Since np.sort() leaves the original array completely untouched, you need to capture its return value in a new variable to actually use the sorted result.

Related Functions

np-argsortnp-lexsortlists