Skip to main content

Python NumPy: How to Use numpy.argsort in Descending Order in Python

The numpy.argsort() function returns the indices that would sort an array in ascending order. However, there is no built-in descending parameter to reverse the sort direction. This is a common need when ranking items from highest to lowest, selecting top-N elements, or sorting data for visualization. This guide covers four practical methods to achieve descending argsort in NumPy, with clear explanations of when to use each.

What numpy.argsort() Returns

Before diving into descending order, it's important to understand what argsort produces:

import numpy as np

arr = np.array([50, 10, 40, 30, 20])

indices = np.argsort(arr)
print("Ascending indices:", indices)
print("Sorted elements:", arr[indices])

Output:

Ascending indices: [1 4 3 2 0]
Sorted elements: [10 20 30 40 50]
note

argsort() does not sort the array itself. It returns an array of indices that, when used to index the original array, produce elements in ascending order. To get descending order, we need to reverse or invert this result.

Method 1: Reversing with a Negative Step [::-1]

The most straightforward approach is to compute the ascending indices first, then reverse them using Python's slice notation [::-1]:

import numpy as np

arr = np.array([5, 2, 8, 3, 6, 10])

# Step 1: Get ascending indices
ascending_indices = arr.argsort()

# Step 2: Reverse to get descending indices
descending_indices = ascending_indices[::-1]

print("Descending indices:", descending_indices)
print("Sorted elements (descending):", arr[descending_indices])

Output:

Descending indices: [5 2 4 0 3 1]
Sorted elements (descending): [10 8 6 5 3 2]

This method is easy to read and works with any data type. The reversal itself is an O(1) operation since [::-1] creates a view of the array rather than a copy.

Method 2: Negating the Array

By negating the array before calling argsort(), the largest values become the smallest (and vice versa), so ascending sort on the negated array produces descending indices for the original:

import numpy as np

arr = np.array([8, 2, 5, 7, 10, 4])

# Negate the array, then argsort - largest original values get smallest negated values
descending_indices = (-arr).argsort()

print("Descending indices:", descending_indices)
print("Sorted elements (descending):", arr[descending_indices])

Output:

Descending indices: [4 0 3 2 5 1]
Sorted elements (descending): [10 8 7 5 4 2]

This method accomplishes everything in a single expression, making it convenient for inline use.

warning

Negation only works reliably with numeric arrays (integers, floats). It does not work with string arrays or other non-numeric types. For non-numeric data, use the slice reversal method ([::-1]) or np.flip() instead.

Method 3: Using np.flip()

np.flip() reverses the elements of an array along the specified axis. Applied to the output of argsort(), it produces descending indices:

import numpy as np

arr = np.array([8, 4, 10, 3, 6])

# Get ascending indices, then flip them
ascending_indices = np.argsort(arr)
descending_indices = np.flip(ascending_indices)

print("Descending indices:", descending_indices)
print("Sorted elements (descending):", arr[descending_indices])

Output:

Descending indices: [2 0 4 1 3]
Sorted elements (descending): [10 8 6 4 3]
tip

np.flip() and [::-1] produce the same result. The difference is stylistic - np.flip() is more explicit and self-documenting, while [::-1] is more concise. For multidimensional arrays, np.flip() also accepts an axis parameter, making it more flexible.

Method 4: Multiplying by -1

This is functionally identical to negation but uses explicit multiplication, which some developers find clearer in intent:

import numpy as np

arr = np.array([4, 1, 5, 7])

descending_indices = (-1 * arr).argsort()

print("Descending indices:", descending_indices)
print("Sorted elements (descending):", arr[descending_indices])

Output:

Descending indices: [3 2 0 1]
Sorted elements (descending): [7 5 4 1]

Like the negation method, this only works with numeric arrays.

Descending argsort on 2D Arrays

All four methods extend naturally to multidimensional arrays. Here's how to sort each row in descending order:

import numpy as np

arr = np.array([
[3, 1, 4],
[1, 5, 9],
[2, 6, 5]
])

# Descending argsort along each row (axis=1)
descending_indices = (-arr).argsort(axis=1)
print("Descending indices per row:")
print(descending_indices)

Output:

Descending indices per row:
[[2 0 1]
[2 1 0]
[1 2 0]]

For the first row [3, 1, 4], the descending order is 4, 3, 1, corresponding to indices [2, 0, 1].

Common Mistake: Confusing Indices with Values

A frequent error is treating the output of argsort() as sorted values rather than indices:

import numpy as np

arr = np.array([50, 10, 40, 30, 20])

# WRONG: these are indices, not sorted values
result = np.argsort(arr)[::-1]
print("These are indices, not values:", result)

Output:

These are indices, not values: [0 2 3 4 1]

The numbers [0, 2, 3, 4, 1] are positions in the original array, not the sorted data itself.

The correct approach to get the actual sorted elements:

import numpy as np

arr = np.array([50, 10, 40, 30, 20])

# CORRECT: use the indices to extract sorted values
descending_indices = np.argsort(arr)[::-1]
sorted_values = arr[descending_indices]
print("Sorted values (descending):", sorted_values)

Output:

Sorted values (descending): [50 40 30 20 10]
danger

np.argsort() returns indices, not values. Always use these indices to index back into the original array if you need the actual sorted elements: arr[descending_indices].

Method Comparison

MethodSyntaxWorks with Non-NumericCreates CopyBest For
Negative step [::-1]arr.argsort()[::-1]YesNo (view)General use - recommended
Negation -arr(-arr).argsort()NoYesSingle-expression inline use
np.flip()np.flip(arr.argsort())YesNo (view)Explicit, multidimensional arrays
Multiply by -1(-1 * arr).argsort()NoYesSame as negation, more explicit

For most cases, reversing with [::-1] is the recommended approach - it is fast, memory-efficient, and works with all data types. Use negation (-arr) when you want a compact single expression for numeric data.