You don't need to sort twice. If you need the indices that sort the array, you'll need np.argsort, but in order to sort the values in arr you only need to index with the result from np.argsort:

s = np.argsort(arr)
# array([1, 0, 2])
arr[s]
# array([1, 2, 3])
Answer from yatu on Stack Overflow
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.5.dev0 Manual
Array of indices that sort a along the specified axis. If a is one-dimensional, a[index_array] yields a sorted a.
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-argsort-in-python
numpy.argsort() in Python - GeeksforGeeks
July 11, 2025 - The default is -1, which sorts along the last axis. kind : [‘quicksort’, ‘mergesort’, ‘heapsort’]Selection algorithm. Default is ‘quicksort’. order : [str or list of str] When arr is an array with fields defined, this argument specifies which fields to compare first, second, etc. Return: [index_array, ndarray] Array of indices that sort arr along the specified axis.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.searchsorted.html
numpy.searchsorted — NumPy v2.2 Manual
>>> a = np.array([40, 10, 20, 30]) >>> sorter = np.argsort(a) >>> sorter array([1, 2, 3, 0]) # Indices that would sort the array 'a' >>> result = np.searchsorted(a, 25, sorter=sorter) >>> result 2 >>> a[sorter[result]] 30 # The element at index 2 of the sorted array is 30.
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › argsort
Python Numpy argsort() - Sort Array Indices | Vultr Docs
November 11, 2024 - Use the argsort() function to get sorted indices. ... import numpy as np data = np.array([10, 1, 5, 3, 8, 6]) sorted_indices = data.argsort() print(sorted_indices) Explain Code
🌐
Programiz
programiz.com › python-programming › numpy › methods › argsort
NumPy argsort()
Index of sorted array on last axis(-1): [[2 0 1] [0 1 2] [0 2 1]] Index of a flattened sorted array: [3 2 6 0 4 8 5 7 1] Index of sorted array on axis 0: [[1 1 0] [2 2 2] [0 0 1]] When sorting on axis 0, rows are compared. The elements in the first column are sorted first followed by the second column and so on. All columns are sorted independently of each other. import numpy as np datatype = [('name', 'U7'), ('age', int), ('height', int)] people = [ ('Alice', 25, 170), ('Bob', 30, 180), ('Charlie', 35, 175) ] array = np.array(people, dtype = datatype)
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.4 Manual
Array of indices that sort a along the specified axis. If a is one-dimensional, a[index_array] yields a sorted a.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-get-the-indices-of-the-sorted-array-using-numpy-in-python
How to Get the Indices of the Sorted Array using NumPy in Python - GeeksforGeeks
September 27, 2025 - The numpy.argsort() function is used to get indices that would sort a NumPy array. Instead of returning sorted array, it returns positions of the elements in order that sorts the array.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.searchsorted.html
numpy.searchsorted — NumPy v2.5.dev0 Manual
Input array. If sorter is None, then it must be sorted in ascending order, otherwise sorter must be an array of indices that sort it. ... Values to insert into a. ... If ‘left’, the index of the first suitable location found is given. If ‘right’, return the last such index.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.searchsorted.html
numpy.searchsorted — NumPy v2.4 Manual
Input array. If sorter is None, then it must be sorted in ascending order, otherwise sorter must be an array of indices that sort it. ... Values to insert into a. ... If ‘left’, the index of the first suitable location found is given. If ‘right’, return the last such index.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.1 Manual
Array of indices that sort a along the specified axis. If a is one-dimensional, a[index_array] yields a sorted a.
🌐
CodingNomads
codingnomads.com › numpy-sort
NumPy Sort: How to Sort a NumPy Array
Use the NumPy argsort command - np.argsort() - to return indices that would sort the array.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.sort.html
numpy.sort — NumPy v2.1 Manual
Sorting algorithm. The default is ‘quicksort’. Note that both ‘stable’ and ‘mergesort’ use timsort or radix sort under the covers and, in general, the actual implementation will vary with data type.
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-sorting-and-searching-exercise-5.php
NumPy: Get the indices of the sorted elements of a given array - w3resource
August 30, 2025 - In this case, i will be a Numpy array containing the indices [0, 3, 4, 6, 1, 5, 2]. These indices indicate the sorted order of the student_id array. ... Generate the indices that would sort a 1D array using np.argsort and return both the sorted array and the indices. Implement a function that, given an unsorted array, returns a new array of indices representing the sorted order. Test the index extraction on arrays with duplicate elements to ensure the order is preserved.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.searchsorted.html
numpy.searchsorted — NumPy v2.1 Manual
Input array. If sorter is None, then it must be sorted in ascending order, otherwise sorter must be an array of indices that sort it. ... Values to insert into a. ... If ‘left’, the index of the first suitable location found is given. If ‘right’, return the last such index.
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.searchsorted.html
numpy.searchsorted — NumPy v2.0 Manual
Input array. If sorter is None, then it must be sorted in ascending order, otherwise sorter must be an array of indices that sort it. ... Values to insert into a. ... If ‘left’, the index of the first suitable location found is given. If ‘right’, return the last such index.
🌐
SciPy
docs.scipy.org › doc › numpy-1.9.2 › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v1.9 Manual
It returns an array of indices of the same shape as a that index data along the given axis in sorted order. ... Describes sorting algorithms used. ... Indirect stable sort with multiple keys. ... Inplace sort. ... Indirect partial sort. ... See sort for notes on the different sorting algorithms. As of NumPy 1.4.0 argsort works with real/complex arrays containing nan values.