outline-style sorting algorithm

Corrado Gioannini gioco at nekhem.com
Mon Apr 19 09:24:18 EDT 2004


On Mon, Apr 19, 2004 at 09:08:59AM -0400, jwsacksteder at ramprecision.com wrote:
> I have a need to sort a list of elements that represent sections of a
> document in dot-separated notation. The built in sort does the wrong thing.
> This seems a rather complex problem and I was hoping someone smarter than me
> had already worked out the best way to approach this. For example, consider
> the following list-
I think you should use a sort function built for this special case.
For example you can sort items comparing map(int, item.split('.'))

>>> cmp(map(int, '1.20.1'.split('.')), map(int, '1.9'.split('.')))
1

that is:

>>> cmp([1,20,1],[1,9])
1

hth,
C.
-- 
                       Thought is only a flash between two long nights,
                                          but this flash is everything.
                                                          (H. Poincaré)






More information about the Python-list mailing list