HELP:sorting list of outline numbers

Scott David Daniels Scott.Daniels at Acm.Org
Wed Aug 3 03:44:36 CEST 2005


Felix Collins wrote:
> Hi All,
> does anyone know any cleaver tricks to sort a list of outline numbers.


For 2.4 and beyond: (using a key function)

     def numparts(outlinetext):
         return [int(number) for number in outlinetext.split('.')]

     lst = ['1', '1.2', '1.12', '1.1', '3.1']
     lst.sort(key=numparts)

For 2.3: (using DSU -- Decorate, Sort, Undecorate)
     def numparts(outlinetext):
         return [int(number) for number in outlinetext.split('.')]

     lst = ['1', '1.2', '1.12', '1.1', '3.1']
     decorated = [(numparts(txt), txt) for txt in lst]
     decorated.sort()
     lst[:] = [txt for code, txt in decorated]

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list