i wanna sort a list that have list in itself
[[2, 'Horror'], [2, 'Romance'], [2, 'Comedy'], [3, 'Action'], [1, 'Adventure'], [2, 'History']]
and want this output that sort first by numbers and then by alphabet:
[[3, 'Action'], [2, 'Comedy'], [2, 'History'], [2, 'Horror'], [2, 'Romance'], [1, 'Adventure']]
but i receive this:
[[3, 'Action'], [2, 'Romance'], [2, 'Horror'], [2, 'History'], [2, 'Comedy'], [1, 'Adventure']]
thanks for answering
>Solution :
We can sort using a lambda function:
lst = [[2, 'Horror'], [2, 'Romance'], [2, 'Comedy'], [3, 'Action'], [1, 'Adventure'], [2, 'History']]
output = sorted(lst, key=lambda x: (-x[0], x[1]))
print(output)
# [[3, 'Action'], [2, 'Comedy'], [2, 'History'], [2, 'Horror'], [2, 'Romance'], [1, 'Adventure']]