sorting - python natsort sort strings recursively -


i find natsort.natsorted sorting order changes part-way through string:

in [31]: import natsort ns in [32]: ns.natsorted(["01-08", "02-07", "01-06", "02-09"]) out[32]: ['01-08', '01-06', '02-09', '02-07'] 

in case, behaviour want is:

in [33]: sorted(["01-08", "02-07", "01-06", "02-09"]) out[33]: ['01-06', '01-08', '02-07', '02-09'] 

try this:

ns.natsorted(["01-08", "02-07", "01-06", "02-09"], alg=ns.ns.int | ns.ns.unsigned) 

the problem natsorted interpreting strings incorrectly. manually sets algorithm unsigned ints. otherwise, searches signed ints, , "-" causes problems (if interpret "-08", example, -8, sorting makes sense).

this equivalent versorted, shortcut algorithm, think it's better explicitly write you're doing, versorted change more applicable versions in future.


Popular posts from this blog