472,791 Members | 1,600 Online

# HELP:sorting list of outline numbers

Hi All,
does anyone know any cleaver tricks to sort a list of outline numbers.

An outline number is a number of the form... 1.2.3

they should be sorted in the following way...

1
1.1
1.2
1.12

python's alpha sort (by design) sorts them...

1
1.1
1.12
1.2

That's no good for me.
I'm planning on splitting the strings into multiple lists of ints and
doing numerical sorts.

Thanks for any clever ideas that might make it easier.

Felix
Aug 3 '05 #1
5 1907
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
Sc***********@Acm.Org
Aug 3 '05 #2
Felix Collins wrote:
Hi All,
does anyone know any cleaver tricks to sort a list of outline numbers.

An outline number is a number of the form... 1.2.3

they should be sorted in the following way...

1
1.1
1.2
1.12

python's alpha sort (by design) sorts them...

1
1.1
1.12
1.2

That's no good for me.
I'm planning on splitting the strings into multiple lists of ints and
doing numerical sorts.

Thanks for any clever ideas that might make it easier.

Use the "key" keyword argument to list.sort().

In [1]: outline = ['1.12', '1.1', '1', '1.2']

In [2]: outline.sort(key=lambda x: map(int, x.split('.')))

In [3]: outline
Out[3]: ['1', '1.1', '1.2', '1.12']

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Aug 3 '05 #3
Robert Kern wrote:
Felix Collins wrote:

Use the "key" keyword argument to list.sort().

In [1]: outline = ['1.12', '1.1', '1', '1.2']

In [2]: outline.sort(key=lambda x: map(int, x.split('.')))

In [3]: outline
Out[3]: ['1', '1.1', '1.2', '1.12']

Is this new in 2.4? I have to use 2.3 as I'm working with Trac.

Traceback (most recent call last):
File "<pyshell#21>", line 1, in -toplevel-
keys.sort(key=lambda x: map(int, x.split('.')))
TypeError: sort() takes no keyword arguments
Thanks Scott and Robert for your quick help. This list is amazing!

Regards,
Felix
Aug 3 '05 #4
Felix Collins wrote:

Thanks Scott and Robert for your quick help. This list is amazing!

Regards,
Felix

Using Decorate, Sort , Undecorate...

works like a charm.

Thanks again.

Felix
Aug 3 '05 #5
Felix Collins wrote:
Using Decorate, Sort , Undecorate...

works like a charm.

As a one-liner, you can also deconstruct and rebuild the outline numbers:

new_outline = ['.'.join(v) for v in (sorted([k.split('.') for k in
old_outline]))]
Aug 3 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.