🚀 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.argsort()

AI & DATA SCIENCE // np-argsort

np.argsort() returns the indices that would sort an array, rather than the sorted values themselves.

Syntax

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

Deep Dive Course

Where np.sort() gives you the sorted values, np.argsort() gives you the order — a set of indices such that indexing the original array with them reproduces np.sort()'s result. This is especially useful for sorting several related arrays in the same consistent order based on values in just one of them: compute the sort order once with argsort() on the key array, then apply that same index order to every related array via fancy indexing.

1Understanding np.argsort()

Where np.sort() gives you the sorted values, np.argsort() gives you the order — a set of indices such that indexing the original array with them reproduces np.sort()'s result. This is especially useful for sorting several related arrays in the same consistent order based on values in just one of them: compute the sort order once with argsort() on the key array, then apply that same index order to every related array via fancy indexing.

💡

Use np.argsort() when you need to sort one array by the values in another — like sorting a list of names by a corresponding list of scores — by computing the index order once with argsort() and applying it to both arrays with fancy indexing.

editor.html
import numpy as np

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

2Practical Example

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

editor.html
import numpy as np

names = np.array(["Charlie", "Alice", "Bob"])
scores = np.array([85, 92, 78])
order = np.argsort(scores)
print(names[order])
localhost:3000

3Best Practices

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

1. Use argsort() to sort several related arrays consistently based on one of them, instead of manually zipping, sorting, and unzipping

2. Reverse an ascending argsort() result with a negative slice, or negate the array before sorting, to get a descending order

3. Use np.sort() directly instead when you only need the sorted values and don't need to apply that same order to other data

⚠️

Tip: Use np.argsort() when you need to sort one array by the values in another — like sorting a list of names by a corresponding list of scores — by computing the index order once with argsort() and applying it to both arrays with fancy indexing.

editor.html
import numpy as np

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

Examples

Example 01Basic Usage
import numpy as np

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

names = np.array(["Charlie", "Alice", "Bob"])
scores = np.array([85, 92, 78])
order = np.argsort(scores)
print(names[order])

Best Practices

  • Use argsort() to sort several related arrays consistently based on one of them, instead of manually zipping, sorting, and unzipping
  • Reverse an ascending argsort() result with a negative slice, or negate the array before sorting, to get a descending order
  • Use np.sort() directly instead when you only need the sorted values and don't need to apply that same order to other data

Interview Question

How would you sort an array of names by their corresponding scores in a separate array, using argsort()?

Hint: Think about computing the sort order once and applying it to both arrays.

Call np.argsort() on the scores array to get the index order that would sort the scores ascending, then use that same index array to reorder the names array with fancy indexing. Since argsort() returns positions rather than values, applying the identical index order to any array that's aligned with the original scores array, whether it's names, IDs, or anything else, reorders it consistently to match how the scores would be sorted, without needing to sort the two arrays together manually.

Exercises

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

arr = np.array([3, 1, 4, 1, 5])
print(np.argsort(arr))

Frequently Asked Questions

How would you sort an array of names by their corresponding scores in a separate array, using argsort()?

Call np.argsort() on the scores array to get the index order that would sort the scores ascending, then use that same index array to reorder the names array with fancy indexing. Since argsort() returns positions rather than values, applying the identical index order to any array that's aligned with the original scores array, whether it's names, IDs, or anything else, reorders it consistently to match how the scores would be sorted, without needing to sort the two arrays together manually.

Related Functions

np-sortnp-lexsortfancy-indexing