a = [(\'dog\', 4, 5), (\'cat\', 1, 3), (\'house\', 6, 3), (\'car\', 1, 1), (\'boyfriend\', 2, 2), (\'doll\', 1, 1)
How can i sort this list in a de
Use the custom key
function returning a tuple where the first item is the sum and the second is the first item of each subelement:
In [2]: sorted(a, key=lambda item: (-(item[1] + item[2]), item[0]))
Out[2]:
[('dog', 4, 5),
('house', 6, 3),
('boyfriend', 2, 2),
('cat', 1, 3),
('car', 1, 1),
('doll', 1, 1)]
Note that we are returning the negative sum to reverse-sort by the sum and keeping the requirement for the secondary key sort to be ascending.