指定reverse = True参数:
list1 = sorted(list1, key=lambda object1: object1.fitness, reverse=True)
演示(简单的整数列表):
>>> l = [6, 0, 2, 3, 1, 5, 4]
>>> sorted(l)
[0, 1, 2, 3, 4, 5, 6]
>>> sorted(l, reverse=True)
[6, 5, 4, 3, 2, 1, 0]
演示(datetime.dates,使用operator.attrgetter代替@SethMMorton建议的lambda):
>>> from datetime import date
>>> from operator import attrgetter
>>> l = [date(2014, 4, 11), date(2014, 4, 2), date(2014, 4, 3), date(2014, 4, 8)]
>>> sorted(l, key=attrgetter('day'))
[datetime.date(2014, 4, 2),
datetime.date(2014, 4, 3),
datetime.date(2014, 4, 8),
datetime.date(2014, 4, 11)]
>>> sorted(l, key=attrgetter('day'), reverse=True)
[datetime.date(2014, 4, 11),
datetime.date(2014, 4, 8),
datetime.date(2014, 4, 3),
datetime.date(2014, 4, 2)]