can't sort

andrew cooke andrew at acooke.org
Sun May 25 21:19:30 EDT 2003


"Anders J. Munch" <andersjm at dancontrol.dk> writes:
> def sort(sequence, cmpfunc=None, project=None):
>     """sort a sequence, returning a new list;
>     if given, cmpfunc(x,y) -> -1, 0, 1;
>     if given, sequence is sorted as would be [project(x) for x in sequence]"""
>     if cmpfunc is not None:
>         assert project is None
>         sorted = list(sequence)
>         sorted.sort(cmpfunc)
>     elif project is not None:
>         intermed = [(project(val), no, val)
>                     for (no,val) in enumerate(sequence)]
>         intermed.sort()
>         return [elem[2] for elem in intermed]
>     else:
>         sorted = list(sequence)
>         sorted.sort()
>     return sorted

are you looking for comments on this particular code, or is it just a
general example?  i don't know whether it should be included in python
or not, but if it was, i'd expect to be able to use cmpfunc and
project together.  also, what is the rank ("no") used for?  stability?

imho, inplace sort seems to be the biggest source of problems for
people who are new to python but otherwise know how to program.  so a
solution of some kind would be a Good Idea.

andrew

-- 
http://www.acooke.org






More information about the Python-list mailing list