argsort (returns the indices that would sort an array)

Returns the indices that would sort an array.
Perform an indirect sort along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as a that index data along the given axis in sorted order.

Syntax

numpy.argsort(<array_like>,<axis>, <kind>,<order>)

array_like Array to sort.
axis int or None, optional. Axis along which to sort. The default is -1 (the last axis). If None, the flattened array is used.
kind {‘quicksort’, ‘mergesort’, ‘heapsort’}, optional Sorting algorithm
order list, optional. When a is an array with fields defined, this argument specifies which fields to compare first, second, etc. Not all fields need be specified.

Returning Value

index_array ndarray, int. Array of indices that sort a along the specified axis. In other words, a[index_array] yields a sorted array

Example

from numpy import array

x = array( [1,6,3,2])
print x.argsort()

Output

[0 3 2 1]