Sorting a list
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Thu Feb 1 16:01:54 EST 2007
John Salerno a écrit :
> Bruno Desthuilliers wrote:
>
>> John Salerno a écrit :
>>
>>> Hi everyone. If I have a list of tuples, and each tuple is in the form:
>>>
>>> (year, text) as in ('1995', 'This is a citation.')
>>>
>>> How can I sort the list so that they are in chronological order based
>>> on the year?
>>
>>
>> Calling sort() on the list should just work.
>
>
> Amazing, it was that easy. :)
A very common Python idiom is "decorate/sort/undecorate", which is just
what you've done here. It's usually faster than passing a custom
comparison callback function (cf a recent thread named "Sorting a List
of Lists, where Paddy posted a link to a benchmark).
More information about the Python-list
mailing list