human
Complete this python program:
def _sorted_factors(factors, method):
assistant
#Here is the Python code you requested.

    """Sort a list of ``(expr, exp)`` pairs.
    """
    if (method == 'sqf'):
        def key(obj):
            (poly, exp) = obj
            rep = poly.rep.rep
            return (exp, len(rep), len(poly.gens), rep)
    else:
        def key(obj):
            (poly, exp) = obj
            rep = poly.rep.rep
            return (len(rep), len(poly.gens), exp, rep)
    return sorted(factors, key=key)