Sort (*, Key=none, Reverse=none)
Key denotes the sort keyword, and the reserve indicates whether it is in reverse order.
The Python3 sort is a stable order.
1. Simple numeric sorting
A = [5,3,2,4,1]
print (a)
a.sort ()
print (a)
2. Simple string sorting
A = [' abc ', ' ACB ', ' AAC ', ' abc ']
print (a)
a.sort ()
print (a)
3. The ordering of the element list
A = [[1,2],[2,1],[1,1],[2,2]]
print (a)
a.sort ()
print (a)
4. Sorting of custom comparison methods
such as the first element in reverse order, the second element positive sequence
Here you use the lambda function. A lambda function is a temporary function, a bit like the C-language macro definition or the inline in C + +, and the parameter must only be a pass value.
The parameters here refer to an element in the list.
a = [[[1,2],[2,1],[1,1],[2,2]]
print (a)
A.sort (Key=lambda x: (-x[0],x[1))
print (a)