python已字典为元素的数组排序

Posted on 2016-08-12 14:27  血染&征袍  阅读(682)  评论(0编辑  收藏  举报
dict = [  
                {'id':'4','name':'b'},  
                {'id':'6','name':'c'},  
                {'id':'3','name':'a'},  
                {'id':'1','name':'g'},  
                {'id':'8','name':'f'}  
            ]  
  
dict.sort(lambda x,y: cmp(x['id'], y['id']))    
dict = sorted(dict, key=lambda x:x['id'])  
  
print dict  
  
>>[{'id': '1', 'name': 'g'}, {'id': '3', 'name': 'a'}, {'id': '4', 'name': 'b'}, {'id': '6', 'name': 'c'}, {'id': '8', 'name': 'f'}]  

 

Copyright © 2024 血染&征袍
Powered by .NET 9.0 on Kubernetes