natural sort order

Natural sort order uses the following rules to sort strings:

1. The space character sorts before numbers; numbers sort before non-numbers. Example: ' 5' < '5' < 'a'
2. Numbers sort in numerical order; leading zeroes and spaces on numbers are ignored, except as a tie-breaker for numbers that have the same numerical value like ' 5' < ' 5 ' < '5' < '05' < '005'
3. Non-numbers sort in the asciibetical order ('Z' before 'a'), like 'A' < 'B' ... < 'Z' < 'a' ... 'z'

Examples

 1. The alphabetically sorted 'd1', 'd10', 'd11', 'd2' in natural sort order: 'd1', 'd2', 'd10', 'd11'.

 2. Example from https://github.com/sourcefrog/natsort/blob/master/example-out.txt. The alphabetically sorted list of the following 29 strings '1-02', '1-2', '1-20', '10-20', 'fred', 'jane', 'pic 7', 'pic 4 else', 'pic 5', 'pic 5 ', 'pic 5 something', 'pic 6', 'pic01', 'pic02', 'pic02000', 'pic02a', 'pic05', 'pic100', 'pic100a', 'pic120', 'pic121', 'pic2', 'pic3', 'pic4', 'tom', 'x2-g8', 'x2-y08', 'x2-y7', 'x8-y8' will appear in natural sort order as:

'1-2', '1-02', '1-20', '10-20', 'fred', 'jane', 'pic01', 'pic2', 'pic02', 'pic02a', 'pic3', 'pic4', 'pic 4 else', 'pic 5', 'pic05', 'pic 5 ', 'pic 5 something', 'pic 6', 'pic 7', 'pic100', 'pic100a', 'pic120', 'pic121', 'pic02000', 'tom', 'x2-g8', 'x2-y7', 'x2-y08', 'x8-y8'.

Links to this page


© djmw 20221023