w3resource

NumPy: Get the indices of the sorted elements of a given array

NumPy Sorting and Searching: Exercise-5 with Solution

Write a NumPy program to get the indices of the sorted elements of a given array.

Sample Solution:

Python Code:

import numpy as np
student_id = np.array([1023, 5202, 6230, 1671, 1682, 5241, 4532])
print("Original array:")
print(student_id)
i = np.argsort(student_id)
print("Indices of the sorted elements of a given array:")
print(i)

Sample Output:

Original array:
[1023 5202 6230 1671 1682 5241 4532]
Indices of the sorted elements of a given array:
[0 3 4 6 1 5 2]

Pictorial Presentation:

NumPy: Get the indices of the sorted elements of a given array.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to sort the student id with increasing height of the students from given students id and height. Print the integer indices that describes the sort order by multiple columns and the sorted data.
Next: Write a NumPy program to sort a given complex array using the real part first, then the imaginary part.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Python: Find the number of occurrence of each values in an iterable

It returns True only if two values point to same object.