sorting ascending/descending with operator.attrgetter

Chris Curvey ccurvey at gmail.com
Wed Mar 31 13:08:50 EDT 2010


I must be having a brain cramp.  Given a list of objects, how can I
sort the list on one attribute in descending order, then sort within
each group in ascending order on another attribute.

For example:

class Foo:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c


I can do "allmyfoos.sort(operator.attrgetter('a','b'))" to sort
ascending on both attributes, but how could i sort by "a, descending,
then b, ascending)?"






More information about the Python-list mailing list