grains.utils.argsorted

grains.utils.argsorted(sequence, reverse=False)[source]

Return the indices that would sort a list or a tuple.

Implementation is taken from https://stackoverflow.com/a/6979121/4892892.

Parameters
  • sequence (list, tuple) – Input sequence in which the sorted indices to be found.

  • reverse (bool) – If set to True, then the elements are sorted as if each comparison was reversed.

Returns

list – List of indices that would sort the input list/tuple.

Examples

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