Sort a list by multiple attributes?

前端 未结 6 2056
后悔当初
后悔当初 2020-11-22 02:48

I have a list of lists:

[[12, \'tall\', \'blue\', 1],
[2, \'short\', \'red\', 9],
[4, \'tall\', \'blue\', 13]]

If I wanted to sort by one e

6条回答
  •  无人共我
    2020-11-22 03:33

    Several years late to the party but I want to both sort on 2 criteria and use reverse=True. In case someone else wants to know how, you can wrap your criteria (functions) in parenthesis:

    s = sorted(my_list, key=lambda i: ( criteria_1(i), criteria_2(i) ), reverse=True)
    

提交回复
热议问题