[Tutor] Sort pointers -

Brian van den Broek bvande at po-box.mcgill.ca
Wed Nov 24 21:31:50 CET 2004


Liam Clarke said unto the world upon 2004-11-24 13:55:
> Hi all,
> 
> Thanks Kent. Question - is Python 2.4 release 1 considered a stable release?
> 
> Brian - Thanks very much for the illuminating functions, they help indeed.
> 
> So when it returns cmp(x,y) it's returning a negative for x < y, 0 for
> x == y, and a positive for x > y...
> 
> eep, a little confused, just trying to understand how Python sorts
> using that. I may go check out Python 2.4...
> 
> Regards,
> 
> 
> Liam Clarke
> 
> 
> On Wed, 24 Nov 2004 11:30:15 -0500, Brian van den Broek
> <bvande at po-box.mcgill.ca> wrote:
> 
>>Kent Johnson said unto the world upon 2004-11-24 06:05:

<SNIP>

Hi Liam,

I'm glad you found my post helpful :-)

For Kent and the other list-members more knowledgeable than I, I'd be 
curious to know more about the "decorate - sort - undecorate idiom (aka 
Schwartzian Transform)" method. -- Oh, strike that, just got Kent's 
post. Thanks :-) (For what it worth, the names the idiom goes by make it 
sound a good deal more dark than it turns out to be!)

Liam, in terms of actually using cmp() to make your own sort functions, 
it really isn't too hard. You've just got to remember which of 1 and -1 
get returned in which circumstances. Or, if you are like me, you've got 
to look it up each time :-(

You can really reduce the work, though. Each time I make a custom cmp() 
function for use with the sort method, I put it into a module -- I call 
mine sorters.py. Having done that, you can promptly forget about the 
details of cmp() again. Just import the desired custom cmp function from 
your sorters module and you are good to go. (Of course, this attitude 
goes a long way to explaining why I have to look up cmp() whenever I 
find the need to write a new comparison function ;-)

You might also want to check out 
<http://www.amk.ca/python/howto/sorting/sorting.html>.

Best,

Brian vdB



More information about the Tutor mailing list