ClickHouse naturalSortKey() Function Fixes Lexicographic Sorting for Version Strings
ClickHouse offers a built-in function called naturalSortKey() that sorts strings containing numbers in human-expected order rather than character-by-character. Standard SQL databases sort strings lexicographically, causing version numbers like 21.11.0 to appear before 21.4.0 and filenames like file10.txt to precede file2.txt. By wrapping a string column in naturalSortKey() within an ORDER BY clause, numeric segments are compared as actual numbers, producing intuitive results. The function works across multiple use cases, including software version numbers, filenames, and multi-part identifiers such as backup labels. It requires no custom parsing logic and can also be applied inside array sorting operations like arraySort() in ClickHouse queries.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in