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'}]

你可能感兴趣的:(python list排序)