Natural Sort (Part 2 of 3)

Before comparing strings we need to identify the text and the numeric parts in the string. The algorithm is very simple: iterate each character of the string, if we find a character that is not a digit, start a Characters chunk, if it’s a digit, create a new Number chunk. Process the next character. If it’s of the same kind than the previous one, then store it in a buffer and continue. If it’s not of the same type, then store the current buffer as a chunk part alongside it’s type, then create a new buffer and set the correct chunk type for it. Repeat the process until the end of the string.

Natural Sort (Part 1 of 3)

Sorting multiple strings means comparing them 2 by 2 and decide which precedes the other. Standard comparison algorithm compare the strings character by character based on their numerical value defined by the string encoding until they find a difference. Then the precedence is defined by the lowest numerical value coming first. Unfortunately, this has a side effect when it comes to sorting numeric values.