Like your comments, float is not the data type you need. In your case, you have an actual hierarchy of chapters / sections.
One simple (and remember, simple is better than complex) way is to represent the partition numbers as tuples. Since tuples are sorted lexicographically, they are naturally sorted in the right order:
>>> lf = [(1, ), (1, 1), (1, 10), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (2, ), (1, 9)] >>> sorted(lf) [(1, ), (1, 1), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (2, )]
As we can see, this also works for tuples with different lengths.
If you want to save partitions as strings, natsort also does fine with dotted values:
>>> s = ['1', '1.1', '1.10', '1.2'] >>> natsort.natsorted(s) ['1', '1.1', '1.2', '1.10']
You can also define your own SectionNumber class, but this is probably overkill.