def cmp_dict(x, y):
    weight_diff = y['weight'] - x['weight']
if weight_diff == 0:
    return y['factor'] - x['factor']
else:
    return weight_diff
myList.sort(cmp_dict)
Ordering a list of dictionaries in python