sorting ascending/descending with operator.attrgetter

Patrick Maupin pmaupin at gmail.com
Wed Mar 31 20:21:18 EDT 2010


On Mar 31, 5:57 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:

> Did you mean list sort method?

Why, yes.  Yes, I did.  Yes, I'm an old forgetful man who sometimes
misspeaks or mistypes, and you're the smartest, sharpest guy in the
world.  Most helpful, too. It's much more useful to show how I'm wrong
than to provide useful info to the OP.  Teaches people not to quickly
post on CLP to try to help out people with immediate problems.  No, it
would have been much more useful for me to self-edit my post for a
couple of days before posting.

Doesn't matter though.  The OP was sorting lists, not strings, so he
knew what I meant.

> > You can also possibly gain some efficiency by using key instead of cmp.
>
> That is an impressive understatement. key is called once for every item,
> cmp is called once for every comparison. cmp has been dropped from Python
> 3.x because its performance is poor.

Yes, well, I did have the "Use a key" statement before "use
cmp" (before I did the tiny bit of editing I did before posting) and
then it occurred to me that he might be sorting with strings, so I
thought I would give him something that worked, and then something
that might work better under some circumstances.

And, BTW, if he is only occasionally
sorting a few short strings^H^H^H^H^H^H^Hlists,
he'll never notice a performance difference.  But at least you can
take comfort that he now knows how superior your intellect is to mine.

Of course, he might accidentally underestimate Peter and Raymond's
intellect.  They published dual sort solutions that are, for most
cases, probably better than the single sort solution I published, but
neglected to call attention to just how bad my suggestion was.



More information about the Python-list mailing list