outline-style sorting algorithm

Terry Reedy tjreedy at udel.edu
Mon Apr 19 14:36:01 EDT 2004


<jwsacksteder at ramprecision.com> wrote in message
news:71650A6F73F1D411BE8000805F65E3CB3B3A4D at SRV-03...
> 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.

Which is to say, the standard alphabetical thing ;-).

> Obviously 1.20.1 should be after 1.9 if we look at this as dot-delimited
> integers, not as decimal numbers.

It it a well known problem that left-justified variable-length number
representations in strings do not sort well when the strings are sorted in
the standard manner.  Even without dots, directory listings will give you

file1
file10
...
file19
file2
file20
...
file29
file3
etc.

One solution is a custom compare or sort algorithm as others have
suggested.  The other is adjustment of the strings by left padding numbers
with spaces or 0s so that they end up right justified to the same column.
Then the standard alphabetical sort does what you want.  For instance,
Windows scandisk, when it recovers fragments, labels them

file001
file002
etc

so that they sort correctly.  In your case, 1.09 would sort before 1.10,
let alone 1.20.  Your choice.

Terry J. Reedy







More information about the Python-list mailing list