import operator


def sortedby2(item_list, *args, **kwargs):
    """sorts ``item_list`` using key_list

    Args:
        item_list (list): list to sort
        *args: multiple lists to sort by
        **kwargs:
            reverse (bool): sort order is descending if True else acscending

    Returns:
        list : ``list_`` sorted by the values of another ``list``. defaults to
        ascending order

    CommandLine:
        python -m utool.util_list --exec-sortedby2 --show

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_list import *  # NOQA
        >>> import utool as ut
        >>> item_list = [1, 2, 3, 4, 5]
        >>> key_list1 = [1, 1, 2, 3, 4]
        >>> key_list2 = [2, 1, 4, 1, 1]
        >>> args = (key_list1, key_list2)
        >>> kwargs = dict(reverse=False)
        >>> result = ut.sortedby2(item_list, *args, **kwargs)
        >>> print(result)
        [2, 1, 3, 4, 5]

    Example:
        >>> # ENABLE_DOCTEST
        >>> # Python 3 Compatibility Test
        >>> import utool as ut
        >>> item_list = [1, 2, 3, 4, 5]
        >>> key_list1 = ['a', 'a', 2, 3, 4]
        >>> key_list2 = ['b', 'a', 4, 1, 1]
        >>> args = (key_list1, key_list2)
        >>> kwargs = dict(reverse=False)
        >>> result = ut.sortedby2(item_list, *args, **kwargs)
        >>> print(result)
        [3, 4, 5, 2, 1]
    """
    assert all([len(item_list) == len_ for len_ in map(len, args)])
    reverse = kwargs.get('reverse', False)
    key = operator.itemgetter(*range(1, len(args) + 1))
    tup_list = list(zip(item_list, *args))
    # print(tup_list)
    try:
        sorted_tups = sorted(tup_list, key=key, reverse=reverse)
    except TypeError:
        # Python 3 does not allow sorting mixed types
        def keyfunc(tup):
            return tuple(map(str, tup[1:]))

        sorted_tups = sorted(tup_list, key=keyfunc, reverse=reverse)
    sorted_list = [tup[0] for tup in sorted_tups]
    return sorted_list