list comprehensions: sorting?

Andrew Henshaw ahenshawatapowerdotcom
Fri Sep 7 19:58:10 EDT 2001


"Alex Martelli" <aleax at aleax.it> wrote in message
news:9na4190avo at enews1.newsguy.com...
> "Clark C . Evans" <cce at clarkevans.com> wrote in message
> news:mailman.999804189.8996.python-list at python.org...
>     ...
> > |     map(process, sorted_by(2,
> > |         [x for x in mylist if x[4]], descend=1))
> >
> > It works... but it's a bit ugly.  I'd like a
> > more "intuitive" syntax... and without having
> > to roll my own sort function.  I would think
> > that sorting lists as part of a list comprehension
> > would be a common activity... I certainly do it
> > often enough.
>
> Some kind or other of sorting is very common, but
> the specific case your "sortdesc" keyword might
> cover if it existed is far too specific and thus
> rare -- sorting on a specific "column" of a
> list of tuples *before* further processing.
>
> I don't recall offhand the last time I had such
> a very specific need -- I sort very frequently,
> but it generally has to be descending on this or
> ascending on that and guaranteed stable, or
> something like that.  And generally I don't
> need the further processing to be done after
> sorting -- it's fine either way.

...snip...

This is somewhat off topic, but I recently posted a cookbook recipe to
Activestate for a class that provides for multiple-key, multiple-direction
sorting.   It's designed to mimic the SQL "ORDER BY" clause so that you can
pass in a list of sort keys (attributes of the class) with optional 'ASC' or
'DESC' modifiers.  It could use some improvements, but I find it quite
useful.

Andrew Henshaw





More information about the Python-list mailing list