def argsort(seq, indices_only=False):
    """Returns the sorted indices and the sorted array (seq) if indices_only=False."""
    if indices_only:
        return sorted(range(len(seq)), key=seq.__getitem__)
    else:
        from operator import itemgetter
        return zip(*sorted(enumerate(seq), key=itemgetter(1)))
