Python: sort an array of dictionaries with custom comparator?

后端 未结 8 1248

I have the following Python array of dictionaries:

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


        
8条回答
  •  一整个雨季
    2021-02-08 11:06

    A hacky way to do it is:

    sorted_master_list = sorted(myarr, key=lambda x: 99999 if x['rank'] == 0 else x['rank'])
    

    This works fairly well if you know your maximum rank.

提交回复
热议问题