Sorting a list

Steven D'Aprano steve at REMOVEME.cybersource.com.au
Thu Feb 1 21:30:44 EST 2007


On Thu, 01 Feb 2007 14:52:03 -0500, John Salerno wrote:

> Bruno Desthuilliers wrote:
> 
>> You don't tell how these lines are formatted, but it's possible that you 
>> don't even need a regexp here. But wrt/ sorting, the list of tuples with 
>> the sort key as first element is one of the best solutions.
> 
> Ah, so simply using sort() will default to the first element of each tuple?

No. It isn't that sort() knows about tuples. sort() knows how to sort a list
by asking the list items to compare themselves, whatever the items are.
Tuples compare themselves by looking at the first element (if any), and
in the event of a tie going on to the second element, then the third, etc.



-- 
Steven D'Aprano 




More information about the Python-list mailing list