argsorted

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

Returns the indices that would sort the iterable.

Parameters:
iterableiterable

The iterable to sort.

keycallable, None, optional

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

reversebool, optional

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

Returns:
sortindiceslist

The indices that would sort the iterable.

Notes

See sorted() for more explanations to the parameters.

Examples

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]