How to use numpy.argsort in Descending order in Python
Last Updated :
28 Apr, 2025
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]
Similar Reads
How to find the Index of value in Numpy Array ? In this article, we are going to find the index of the elements present in a Numpy array.Using where() Methodwhere() method is used to specify the index of a particular element specified in the condition.Syntax: numpy.where(condition[, x, y])Example 1: Get index positions of a given valueHere, we fi
5 min read
How to Sort a Set of Values in Python? Sorting means arranging the set of values in either an increasing or decreasing manner. There are various methods to sort values in Python. We can store a set or group of values using various data structures such as list, tuples, dictionaries which depends on the data we are storing. We can sort val
7 min read
How to Sort a Multi-dimensional Array by Value What is Sorting?Arranging data in an increasing or decreasing fashion according to their values is called Sorting. Below are shown some processes for sorting arrays of several dimensions. Sorting a 1-Dimensional array:We can sort any Dimensional array using the sort method in C++. Syntax: sort(arr,
11 min read
Sort a list of objects by multiple attributes in Python In this article, we are going to learn how to sort a list by multiple attributes with Python. Introduction Python is a dynamically typed language that offers numerous data types, such as list, tuple, set, dictionary, etc. and sorting is the most commonly used operation on any data structures such as
7 min read
Sort a list in Python without sort Function Sorting a list in Python means arranging the elements in a specific order (ascending or descending) using custom logic. For example, in a list like [5, 2, 9, 1], we can manually rearrange the elements as [1, 2, 5, 9]. Letâs explore different methods of sorting a list without using built-in sort func
3 min read
Python | Sort the list alphabetically in a dictionary In Python Dictionary is quite a useful data structure, which is usually used to hash a particular key with value, so that they can be retrieved efficiently. Let's see how to sort the list alphabetically in a dictionary. Sort a List Alphabetically in PythonIn Python, Sorting a List Alphabetically is
3 min read