If you've ever sorted software version numbers like 21.4.0, 21.11.0, or filenames such as file1.txt, file10.txt, and file2.txt, you've probably noticed something frustrating.
The results don't look right.
Instead of ordering values the way humans naturally expect, SQL databases compare strings character by character. As a result, 21.11.0 may appear before 21.4.0, and file10.txt often comes before file2.txt.
This behavior is technically correct—but rarely useful.
ClickHouse® provides the naturalSortKey() function specifically to solve this problem. Although it's a relatively small utility function, it can save you from writing complicated parsing logic or performing application-side sorting whenever numbers are embedded inside strings.






