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 OverflowVideos
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])
Yes, you can use argsort to get the sorted array.
import numpy as np
arr = np.array([2,1,3])
sorted_indices = np.argsort(arr)
sorted_arr = arr[sorted_indices]
Yes, there's the x = numpy.argsort(a) function or x = numpy.ndarray.argsort(a) method. It does exactly what you're asking for. You can also call argsort as a method on an ndarray object like so: a.argsort().
Here's a link to the documentation: http://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html#numpy.argsort
Here's an example, for reference and convenience:
# create an array
a = np.array([5,2,3])
# np.sort - returns the array, sorted
np.sort(a)
>>> array([2, 3, 5])
# argsort - returns the original indexes of the sorted array
np.argsort(a)
>>> array([1, 2, 0])