Text sorting places character sequences in lexical order. Cast numeric text deliberately when the ordering represents a quantity.
Worked example
WITH labels(v) AS (VALUES ('2'),('10'),('1')) SELECT v FROM labels ORDER BY cast(v AS INTEGER);Result
[
{
"v": "1"
},
{
"v": "2"
},
{
"v": "10"
}
]Implementation notes
Casting malformed numeric text can produce a plausible numeric result. Validate imported fields before treating the sort order as proof of clean data.
Try it yourself
Sort the strings 2, 10 and 02 and decide how to resolve numeric ties.
Open the SQL example →Editorial date January 21, 2026 · Published December 6, 2026