Sorting Lists

Tom Bryan tbryan at python.net
Fri Jul 27 18:19:05 CEST 2001


phawkins at connact.com wrote:

>>>>>> "TB" == Tom Bryan <tbryan at python.net> writes:
> 
> TB> While it's probably much harder to read, one could
> TB> avoid copying the list twice and maintain the semantics
> TB> of sorting in place by doing something like the following...
> 
> TB> def sort_by_field( index ):
> TB>     def cmpfunc( x, y, i=index ):
> TB>         return cmp( x[i], y[i] )
> TB>     return cmpfunc
> 
> TB> data.sort( sort_by_field( 2 ) )
> 
> TB> ---Tom
> 
> That's what I thought, but when I timed it, I found that this method
> is slower, because of the repeat function calls.  

Of course, I never claimed that it was faster.  One simply avoids 
copying the list and sorts in place rather than returning a sorted 
copy of the list. :-)

By the way, would you care to post the results of your performances 
tests.  Is it faster for lists of all sizes?  How does the speed difference 
change as the size of the list changs?  

you-can't-get-away-that-easily-ly yours
---Tom




More information about the Python-list mailing list