Forum Discussion
Thanks Robb, good clarification - it is alphanumeric, not natural.
EricOsmanski Alphanumeric and Natural sorting are essentially the same and given by the code example above. OneStream is using Lexical sorting. While not wanting to split hairs here, the distinction is important. Humans are consuming and interacting with the UI. IMO its important the UI is a humanistic experience.
Computers generally use Lexical sorting by default. This is where strings are compared character by character from left to right, using the ASCII values of the characters. Lexical sorting is efficient and fine when code needs to keep sets of elements sorted the same way for comparison and parsing.
The problem arises from comparing strings with numbers as a collection of ascii characters instead of as a number comprised of those parts The code provided in the AlphaNumericComparator class in this post overcomes this limitation of lexical sorting described here:
List of string to sort:
"item10"
"item1"
"item20"
"item2"
"item3"
Lexical Character-by-Character Comparison:
"i" in all strings is the same.
"t" in all strings is the same.
"e" in all strings is the same.
"m" in all strings is the same.
The comparison starts to differ at the first digit.
Comparing Digits:
"1" in "item10" vs. "1" in "item1" – They are the same, so the next character is compared.
"0" in "item10" vs. "1" in "item1" – "0" comes before "1" in ASCII, so "item10" is sorted before "item1".
Next, "item1" is compared with "item20". "1" (from "item1") comes before "2" (from "item20") in ASCII, so "item1" is sorted before "item20".
Similarly, "item2" comes before "item3".
Related Content
- 11 months ago
- 8 months ago
- 3 years ago
- 2 years ago