Sorting on multiple values, some ascending, some descending

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Wed Jan 3 15:10:55 EST 2007


Raymond Hettinger:
> The simplest way is to take advantage of sort-stability and do
> successive sorts.  For example, to sort by a primary key ascending and
> a secondary key decending:
>    L.sort(key=lambda r: r.secondary, reverse=True)
>    L.sort(key=lambda r: r.primary)

That's probably the faster and simpler way.
The following solution is probably slow, memory-hungry, and it's not
tested much so it may be buggy too, but it shows my idea, and bugs can
be fixed:

class Inverter:
    def __init__(self, item, reversed=False):
            self.item = item
            self.reversed = reversed
    def __cmp__(self, other):
        if self.reversed:
            return cmp(other.item, self.item)
        else:
            return cmp(self.item, other.item)

data = [[1, "a", "b"], [1, "b", "d"], [1, "d", "a"]]
reverses = [True, False, False]

print sorted(data, key=lambda subseq: map(Inverter, subseq, reverses))

Bye,
bearophile




More information about the Python-list mailing list