can't sort
Dave Benjamin
ramen at lackingtalent.com
Sun May 25 17:11:54 EDT 2003
In article <3ecd0282$0$76090$edfadb0f at dread11.news.tele.dk>, Anders J. Munch wrote:
> Suppose we added the following function to the builtins. Or at least,
> to a standard module.
>
> 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
>
> This function sorts arbitrary sequences (presuming enumerate works for
> unsized sequences, haven't tried out 2.3 so I don't know for sure),
> and always returns a new list.
>
> For good measure it also implements the DSU pattern if you provide a
> projection function.
+1
More information about the Python-list
mailing list