The data style is as follows , Nested dictionary for list
example = [
{
'id':1,'name':'b'},
{
'id':2,'name':'a'}
]
Although the basic principles have been understood , Still want to be wordy , In computer language , There is such a logical relationship
>>> 2 > 1
True
>>> 'a' < 'b'
True
Can solve your worries
key
Parameter determines the sort field reverse
Parameters determine ascending and descending order >>> sorted(example,key=lambda x:x['id'],reverse=False)
[{
'id': 1, 'name': 'b'}, {
'id': 2, 'name': 'a'}]
>>> sorted(example,key=lambda x:x['id'],reverse=True)
[{
'id': 2, 'name': 'a'}, {
'id': 1, 'name': 'b'}]
>>> sorted(example,key=lambda x:x['name'],reverse=False)
[{
'id': 2, 'name': 'a'}, {
'id': 1, 'name': 'b'}]
>>> sorted(example,key=lambda x:x['name'],reverse=True]
[{
'id': 1, 'name': 'b'}, {
'id': 2, 'name': 'a'}]
>>> example
[{
'id': 1, 'name': 'b'}, {
'id': 2, 'name': 'a'}]
There was no change
lambda Pass in the parameter : Output results
Equivalent to
def Isn't there a name ( Pass in the parameter ):
return Output results