persons= [{"name":" Jon"," age": 32}, {"name":" Alan"," age": 50}, {"name":"Bob"," age":23}]
sorted (persons, key = lambda x: (x ["name"],-x [" age"])
persons
[{"age": 32," name": "Jon"},
{" age": 50, "name":" Alan"},
{"age": 23," name": "Bob"}]
output result:
[{"age": 50," name": "Alan"},
{" age": 23, "name":" Bob"},
{"age": 32," name": "Jon"}]
this is an explanation of the sorted function. I know that key receives a function here, but I can"t understand how to sort the x and-x in it. I hope the seniors can give me some advice and thank you in advance.