How to use numpy.argsort in Descending order in Python

The numpy.argsort() function is used to conduct an indirect sort along the provided axis using the kind keyword-specified algorithm. It returns an array of indices of the same shape as arr, which would be used to sort the array. It refers to value indices ordered in ascending order. In this article, we will see how we can use NumPy argsort for sorting in descending order.
Python NumPy argsort Descending Order
Below are the ways and methods by which we can use NumPy argsort to sort in descending order in Python:
- Using Negative Step
- Negating the Array
- Using np.flip()
- Using Multiplication by ‘-1’
Python NumPy argsort using Negative Step
In this example, we are using a negative step to sort NumPy Array using numpy.argsort(). Here, we will use argsort() to get the indices that would sort the original array in ascending order. We will apply slicing [ : : -1] to reverse these ascending indices. These are the indices corresponding to the elements in descending order. Finally, we use these descending indices to retrieve the elements from the original array in descending order.
Python3
import numpy as nparr = np.array([5, 2, 8, 3, 6, 10])Â
# get the indices that would sort the array in ascending orderascending_indices = arr.argsort() Â # [1 3 0 4 2 5]Â
# reverse the ascending indices to get descending indicesdescending_indices = ascending_indices[::-1] Â # [5 2 4 0 3 1]Â
# use the descending indices to get the elements in descending ordersorted_elements_descending = arr[descending_indices] Â # [10 8 6 5 3Â 2]Â
print("Sorted Indices (Descending):", descending_indices)print("Sorted Elements (Descending):", sorted_elements_descending) |
Output
Sorted Indices (Descending): [5 2 4 0 3 1]
Sorted Elements (Descending): [10 8 6 5 3 2]
Descending by Negating the NumPy Array
In this example, we are negating the NumPy Array to sort it in descending order using numy.argsort(). Here, we will use argsort() to get the indices that would sort the original array in ascending order. Now, to obtain the indices that sort the array in descending order, we first negate the array using –arr and then use argsort() on the negated array to get the indices in descending order.
Python3
import numpy as nparr = np.array([8, 2, 5, 7, 10, 4])Â
# get the indices that would sort the array in ascending orderascending_indices = arr.argsort()Â
# getting the indices that sort the array in descending order by negating the array and sorting itdescending_indices = (-arr).argsort()Â
# Use the descending indices to get the elements in descending ordersorted_elements_descending = arr[descending_indices]Â
print("Sorted Indices (Descending):",descending_indices)print("Sorted Elements (Descending):",sorted_elements_descending) |
Output
Sorted Indices (Descending): [4 0 3 2 5 1]
Sorted Elements (Descending): [10 8 7 5 4 2]
Python NumPy Array Descending argsort using NumPy flip()
In this example, we are using numpy.flip() to use argsort in descending order. Here, we will use argsort() to get the indices that would sort the array in ascending order. Then, we use np.flip() to reverse the order of these indices which represents the order in which the elements of array should be sorted in descending order. Finally, use these sorted indices to extract the elements from original array, giving us the sorted elements in descending order.
Python3
import numpy as np Â
arr = np.array([8, 4, 10, 3, 6])Â
# get the indices that sort the array in ascending ordersorted_indices = np.argsort(arr)Â
# reversing the order of elementssorted_indices_descending = np.flip(sorted_indices)Â
# get the elements corresponding to sorted indices in descending ordersorted_elements = arr[sorted_indices_descending]Â
print("Sorted Indices (Descending):",sorted_indices_descending)print("Sorted Elements (Descending):",sorted_elements) |
Output
Sorted Indices (Descending): [2 0 4 1 3]
Sorted Elements (Descending): [10 8 6 4 3]
Multiplication by ‘-1’
In this example, we are multiplying by -1 to use agrsort in descending order.
Python3
import numpy as nparr = np.array([4, 1, 5, 7])Â
sorted_indices = arr.argsort()sorted_indices_descending = (-1*arr).argsort()Â
sorted_elements= arr[sorted_indices_descending]Â
print("Sorted Indices (Descending):",sorted_indices_descending)print("Sorted Elements (Descending):",sorted_elements) |
Output
Sorted Indices (Descending): [3 2 0 1]
Sorted Elements (Descending): [7 5 4 1]



