This function is implemented to sort the iterable of an iterative object . The optional parameter key is a function of the comparison key;Reverse is a Boolean value that indicates whether to reverse-arrange the items in the object.
Example:
#sorted () print (sorted ([5, 2, 3, 1, 4])) print (sorted ({1: ' D ', 2: ' B ', 3: ' B ', 4: ' E ', 5: ' A '}, reverse = True)) print (sorted ("This was a test string from Andrew". Split (), Key=str.lower)) Student_tuples = [ (' John ', ' A ', '), (' Jane ', ' B ', '), (' Dave ', ' B ', '), ]print (sorted (Student_tuples, Key=lambda student:student[2]) # sorted by age
The resulting output is as follows:
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
[' A ', ' Andrew ', ' from ', ' was ', ' string ', ' test ', ' this ']
[(' Jane ', ' B ', ' + '), (' John ', ' A ', '), (' Dave ', ' B ', 100)]
Cai Junsheng qq:9073204 Shenzhen
Python standard library: Built-in function sorted (iterable[, key][, reverse])