Friday, October 10, 2025
HomeLanguagesHow to use numpy.argsort in Descending order in Python

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 np
arr = np.array([5, 2, 8, 3, 6, 10])
 
# get the indices that would sort the array in ascending order
ascending_indices = arr.argsort()  # [1 3 0 4 2 5]
 
# reverse the ascending indices to get descending indices
descending_indices = ascending_indices[::-1]  # [5 2 4 0 3 1]
 
# use the descending indices to get the elements in descending order
sorted_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 np
arr = np.array([8, 2, 5, 7, 10, 4])
 
# get the indices that would sort the array in ascending order
ascending_indices = arr.argsort()
 
# getting the indices that sort the array in descending order by negating the array and sorting it
descending_indices = (-arr).argsort()
 
# Use the descending indices to get the elements in descending order
sorted_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 order
sorted_indices = np.argsort(arr)
 
# reversing the order of elements
sorted_indices_descending = np.flip(sorted_indices)
 
# get the elements corresponding to sorted indices in descending order
sorted_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 np
arr = 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]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS