python对字典集合进行排序

2017-04-01  本文已影响0人  那脸憔悴
#coding:utf-8
def test():
    m = [{'x':20, 'value':'aaaaa'},{'x':15, 'value':'bbbbb'},{'x':22, 'value':'abcdefg'},{'x':30, 'value':'gggggg'}]
    print m
    result = sorted(m, key = sort_test) #顺序
    #result = sorted(m, key = sort_test, reverse = True) #倒序
    return result
def sort_test(c):
    return c["x"] #按字典的''x''字段进行排序

print test()
上一篇 下一篇

猜你喜欢

热点阅读