how can I sort a bunch of lists over multiple fields?

Lonnie Princehouse finite.automaton at gmail.com
Wed May 4 18:01:08 EDT 2005


> If you want to compare partial keys, often the simplest approach is
to
> use tuples as keys, with the elements of the tuple being the "parts"
of
> the key.  Python's standard tuple comparison mechanism takes care of
the
> rest.

Right, but suppose it's expensive to generate each part of the key -
e.g. a database query or some kind of hash, so it doesn't make sense to
compute the whole key in advance if the first part is sufficient to
establish an ordering most of the time.   This is what I had in mind (I
should have been more clear about that)

Sure, you could put together a lazy evaluation object to use for
elements of the tuple, but this again becomes equivalent to writing a
__cmp__ function...




More information about the Python-list mailing list