python list排序


data_list = []
data_list.append({'softname':'1','version':'1.2.2.2'})
data_list.append({'softname':'7','version':'1.2.2.2'})
data_list.append({'softname':'5','version':'1.2.2.2'})
data_list.append({'softname':'2','version':'1.2.2.2'})
data_list.append({'softname':'3','version':'1.2.2.2'})
data_list.append({'softname':'9','version':'1.2.2.2'})
#升序
data_list.sort(key=lambda obj:obj.get('softname'), reverse=False)

print data_list
#[{'softname': '1'}, {'softname': '2'}, {'softname': '3'}, {'softname': '5'}, {'softname': '7'}, {'softname': '9'}]
#降序
data_list.sort(key=lambda obj:obj.get('softname'), reverse=True)
print data_list
#[{'softname': '9'}, {'softname': '7'}, {'softname': '5'}, {'softname': '3'}, {'softname': '2'}, {'softname': '1'}]

posted @ 2012-04-25 18:00  lhj588  阅读(4020)  评论(0编辑  收藏  举报