Sorting list (maybe stupid)

Eric Brunel eric.brunel at pragmadev.com
Tue Jun 11 08:06:05 EDT 2002


Charl P. Botha wrote:

> In article <slrnagbh7m.h03.jarvin24 at lehtori.cc.tut.fi>, Jarvinen Petri
> wrote:
>> I have a problem and can't solve it:
>> 
>> There is lines of text like:
>> 
>> 2. Header2
>> 1. Header1
>> 102. Header102
>> 
>> and so on...
>> 
>> And I need to sort them according to the number (1,2,3,4...)
> 
> Put each line in a tuple, first element the number, second element the
> header.  Make a list of these tuples, and then use list.sort() with a comp
> function that compares using the first element in the tuple.

I think it's useless. Just try:

l = [(102, 'foo'), (2, 'bar'), (1, 'baz')]
l.sort()
print l

Apparently, the default sort function already sorts tuples according to 
their first element...
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com



More information about the Python-list mailing list