iteration_utilities.argsorted(iterable, key=None, reverse=False)

Returns the indices that would sort the iterable.

iterable : iterable

The iterable to sort.

key : callable, None, optional

If None sort the items in the iterable, otherwise sort the key(items). Default is None.

reverse : bool, optional

If False sort the iterable in increasing order otherwise in decreasing order. Default is False.

sortindices : list

The indices that would sort the iterable.


See sorted() for more explanations to the parameters.


To get the indices that would sort a sequence in increasing order:

>>> from iteration_utilities import argsorted
>>> argsorted([3, 1, 2])
[1, 2, 0]

It also works when sorting in decreasing order:

>>> argsorted([3, 1, 2], reverse=True)
[0, 2, 1]

And when applying a key function:

>>> argsorted([3, 1, -2], key=abs)
[1, 2, 0]