{sortid:number}

Description

Builds a text sort key from a number so that numbers sort correctly when compared as plain text. In a plain alphabetical (lexicographic) sort, "10" comes before "9" because the engine compares character by character and "1" is less than "9". The sortid command fixes that by prefixing each number with a single letter that encodes its length: a 1-digit number gets "B", a 2-digit number gets "C", a 4-digit number gets "E", and so on (the letter is chr(65 + number of characters), so an empty input gives just "A"). Shorter numbers therefore always sort before longer ones, and numbers of the same length sort by value. So 1, 10 and 89 become B1, C10 and C89, which sort in the right numeric order as text. This is the per-level building block that the sortstring command uses to order an item tree.

Parameters

number optional default (empty)

The number (or text) to turn into a sort key. The output is one letter encoding the length followed by the value unchanged: a 1-digit value gives "B" + value, 2 digits give "C" + value, and so on. Non-digit text works too - it is measured by character count, not parsed as a number. If you omit it the result is just "A" (length 0).

Examples

test{sortid:9}
ExpectedB9
ActualB9
One digit has length 1, so the key starts with B (the letter after A). The value is appended unchanged.
test{sortid:10}
ExpectedC10
ActualC10
Two digits have length 2, so the key starts with C. Because C sorts after the B of any one-digit key, 10 now sorts after 9 - which is what you want.
test{sortid:2458}
ExpectedE2458
ActualE2458
Four digits have length 4, so the letter is E (A=0, B=1, C=2, D=3, E=4). The more digits a number has, the later its leading letter.
test{sortid:9}-{sortid:10}-{sortid:100}
ExpectedB9-C10-D100
ActualB9-C10-D100
The point of the command. Sorted as plain text, raw 9, 10, 100 would order as 10, 100, 9. The keys B9, C10, D100 sort B before C before D, so the original numeric order is preserved.
test{sortid:abc}
ExpectedDabc
ActualDabc
The input is not parsed as a number - only its character count matters. abc has length 3, so the letter is D and the text is appended unchanged.
test[{sortid:}]
Expected[A]
Actual[A]
With no value the length is 0, so the result is just A (chr(65)). Useful to know as the lower bound of the key space; an empty value sorts before everything else.