In this article, you will learn how to sort indexes based on values in Numpy using the argsort() function.

## What is the argsort() function?

The argsort() function in Numpy returns the indices of an array sorted by its values. The default is to sort the values in ascending order, but you can also specify descending order by passing the reverse=True argument.

## How to sort indexes based on values

To sort indexes based on values in Numpy, you can use the following steps:

Import the numpy library.

Create an array of values.

Call the argsort() function on the array.

The argsort() function will return an array of indices, where the element at each index is the original index of the corresponding sorted value.

Example:

Indexes of sorted values have been returned.

This means that the values in the original array are sorted in ascending order, and the indexes correspond to the original indices of the sorted values.

## Sorting multidimensional arrays

You can also sort indexes based on values in multidimensional arrays. To do this, you can pass the axis argument to the argsort() function. The axis argument specifies the axis along which the sorting should be performed.

For example, the following code sorts the indexes of a 2D array based on the values in the first dimension:

import numpy as np my_list = np.array([5, 7, 1, 3, 2, 8]).reshape(3, 2) print(f"This is my array: \n {my_list}") indexes_sorted = my_list.argsort() print(f"There are indexes where you can find my values" f" in ascending order: \n {indexes_sorted}")

This means that the values in the first dimension of the original array are sorted in ascending order, and the indexes 0, 1, 2, and 2 correspond to the original indices of the sorted values in the first dimension.

## Sorting Different Axes

The argsort() function can also be used to sort indexes based on values in different axes of a multidimensional array. The axis argument specifies the axis along which the sorting should be performed.

By default, the argsort() function sorts based on the last axis of the array. This means that if you have a 2D array, the values in the second dimension will be sorted.

To sort the indexes based on the values in the first dimension, you can pass the axis=0 argument to the argsort() function.

For example, the following code sorts the indexes of a 2D array based on the values in the first dimension:

import numpy as np my_list = np.array([5, 7, 1, 3, 2, 8]).reshape(3, 2) print(f"This is my 2d array: \n {my_list}") indexes_sorted = my_list.argsort(axis=0) print(f"There are indexes where you can find my values" f" in ascending order on 0 axis: \n {indexes_sorted}")

This means that the values in the first dimension of the original array are sorted in ascending order, and the indexes correspond to the original indices of the sorted values in the first dimension.

You can also sort the indexes based on the values in the second dimension by passing the axis=1 argument to the argsort() function.

For example, the following code sorts the indexes of a 2D array based on the values in the second dimension:

import numpy as np my_list = np.array([5, 7, 1, 3, 2, 8]).reshape(3, 2) print(f"This is my 2d array: \n {my_list}") indexes_sorted = my_list.argsort(axis=1) print(f"There are indexes where you can find my values" f" in ascending order on 1 axis: \n {indexes_sorted}")

This means that the values in the second dimension of the original array are sorted in ascending order, and the indexes correspond to the original indices of the sorted values in the second dimension.

## Key Takeaways

- The `argsort()` function in Numpy returns the indices of an array sorted by its values.
- The default is to sort the values in ascending order, but you can also specify descending order by passing the `reverse=True` argument.
- You can use the `axis` argument to specify the axis along which the sorting should be performed.

## FAQ

- Q: What is the difference between the `argsort()` function and the `sort()` function?
- A: The `argsort()` function only returns the indices of the sorted array, while the `sort()` function sorts the array in place.
- Q: How can I sort the indexes of a multidimensional array?
- A: You can use the `axis` argument to specify the axis along which the sorting should be performed.
- Q: What are some other ways to sort indexes in Numpy?
- A: You can also use the `sort_index()` function, which is a more general function that can be used to sort any sequence of data.