python dictObject-list 排序
对dictObject list进行排序
dict = [{'x': 1, 'y':3}, {'x': 2, 'y':4}]
* x 的值大于2优先排
* y 按照从小到大顺序排列
for d in dict: d.is_x_more_than_two = True if d.x >2 else False dict.sort(key: lambda d: (not d.is_x_more_than_two, d.y))
* x 的值大于2优先排
* y 按照从大到小顺序排列
for d in dict: d.is_x_more_than_two = True if d.x >2 else False dict.sort(key: lambda d: (d.is_x_more_than_two, d.y), reverse=True)
在sort函数中,当reverse=False时(默认情况),按从小到大顺序排列 , 如果出现boolean, 先排False, 再排True (我习惯理解为False为0, True为1)。