Python: sort an array of dictionaries with custom comparator?

后端 未结 8 1232

I have the following Python array of dictionaries:

myarr = [ { \'name\': \'Richard\', \'rank\': 1 },
{ \'name\': \'Reuben\', \'rank\': 4 },
{ \'name\': \'Reece\'         


        
8条回答
  •  醉酒成梦
    2021-02-08 11:09

    I'd do

     sortedlist = sorted([x for x in myarr if x['rank']], key=lambda x: x['rank']) + [x for x in myarr if not x['rank']]
    

    bit I guess it could be compressed somehow.

提交回复
热议问题