sorted(__iterable,key,reverse), Can carry three parameters
1. Iteratable object
2. Sort of key
3. Ascending descending sequence
# Without parameters , Default ascending order
>>>s = [5,3,4,1,2]
sorted(s)
[1, 2, 3, 4, 5]
# Add reverse=True, Descending order
sorted(s, reverse=True)
[5, 4, 3, 2, 1]
>>>s = [[4,5,6,'d'],[1,3,2,'a'],[6,7,8, 'b'], [9,3,2,'c']]
# sorted By default, the first value of the list element is arranged in ascending order
>>>sorted(s)
[[1, 3, 2, 'a'], [4, 5, 6, 'd'], [6, 7, 8, 'b'], [9, 3, 2, 'c']]
# reflection , If we want to compare the elements in the fourth column a,b,c,d Sort , One line of code teaches you to simply implement
>>>sorted(s, key=lambda x:x[3])
[[1, 3, 2, 'a'], [6, 7, 8, 'b'], [9, 3, 2, 'c'], [4, 5, 6, 'd']]
Empathy , I want to sort the second column
>>>sorted(s, key=lambda x:x[1])
[[1, 3, 2, 'a'], [9, 3, 2, 'c'], [4, 5, 6, 'd'], [6, 7, 8, 'b']]
# The default is to sort keys
>>>s = {'c':1,'b':2,'a':3}
>>>sorted(s)
['a', 'b', 'c']
# Yes value Arrange
>>>sorted(s.items(),key=lambda x:x[1], reverse=True)
[('a', 3), ('b', 2), ('c', 1)]