python中的json数组排序,取最小最大值


from operator import itemgetter
json_array = [{"time": 20150312, "value": "c"}, {"time": 20150301, "value": "a"}, {"time": 20150305, "value": "b"}]

json_array.sort(key=lambda x: x["time"], reverse=False) # reverse=False 升序,True是降序
#或使用sorted函数
# new_json_array= sorted(json_array,key=lambda x:x["time"]) # 升序
print(sorted(json_array, key=itemgetter('value'), reverse=True))  # 降序,需要导入:from operator import itemgetter

# 根据某个key的最小值取
print(min(json_array, key=itemgetter('value')))
# 取最大值
print(max(json_array, key=itemgetter('time')))
print(json_array)
posted @ 2023-03-09 11:45  dyjnicole  阅读(282)  评论(0编辑  收藏  举报