sorting for humans


how would you go about implementing natural string sort in python?

natural sort order – it’s like encryption. everyone tries to roll their own, and it never works out well (if you didn’t follow the link above, jeff atwood said it best).

don’t reinvent the wheel unless you really have to. for the code in post linked above, i’d say, “don’t forget about capital letters”, and also, “length is not as important as you’d think”. imagine, for example, this [low-end,value,high-end] setup for the range:


is m15 in that range or not?

(John, from seeknuance, observes in the comments that this and other cases are not relevant for his specific implementation; he can afford to stay much simpler, and more efficient, than my code below)

anyways, this is what i’d do:

import re

def in_natural_range(low, value, high):
    result = []
    for s in [low,value,high]:
        result.append( [int(c) if c.isdigit() else c.lower() for c in re.split('([0-9]+)', s)])
    return ''.join([str(i) for i in sorted(result)[1]]) == value.lower() #fixed based on comment below

print in_natural_range('a','b','c')
print in_natural_range('1','2','3')
print in_natural_range('a1','b','c1')
print in_natural_range('a1','a1','a1')
print in_natural_range('a1','a2','a3')
print in_natural_range('a1','a22','a3')
print in_natural_range('11','12','13')
print in_natural_range('11','123','13')

well, if i had to i’d do that. in practice, i’d just stick to python’s natsort library.

[More programming riddles]