from functools import cmp_to_key


def argsort(mylist, comp=None):
    """Returns the indices that sort a list.

    Parameters
    ----------
    mylist : list of objects
        List to sort.
    comp : function, optional
        A comparison function used
        to compare two objects in the list.
        Defaults to None.

    Returns
    -------
    list of int
        The permutation that sorts the list.
    """

    # Based on https://stackoverflow.com/questions/3382352/equivalent-of-numpy-argsort-in-basic-python

    if comp is None:
        return sorted(range(len(mylist)), key=mylist.__getitem__)
    else:
        return sorted(range(len(mylist)), key=cmp_to_key(comp))