python集合的排序主要使用cmp
关键字来定义比对函数,以下针对几个较为典型的类型列举出对应的方法:
正序排列
普通一维数组(数值、字符串数组)
num = sorted(num)
对象数组(需要指定字段排序)
cursor = sorted(cursor, cmp=lambda x, y: cmp(x['count'], y['count']))
倒序排列
cursor = sorted(cursor, cmp=lambda x, y: cmp(y['count'], x['count']))
指定字段排序
cursor = sorted(cursor, key=lambda x: (x['userId'], x['date']))