PG Bug reporting form <noreply@postgresql.org> writes:
> I don't know if there is anything specific to do for having a proper ordered
> output, but the result seems wrong to me.
You need to use a locale (collation) that matches your expectations.
FWIW, I get the same ordering you show with COLLATE "en_US" on a
Linux box, and it's likely that glibc would use similar ordering
rules for some other languages. The behavior you say you want
looks like it'd match COLLATE "C", though that might act a little
funny with non-ASCII characters.
(I'm not here to defend glibc's sorting rules, but I think what
they're doing in this example is ignoring spaces in the first pass.)
You can find more about this topic in
https://www.postgresql.org/docs/current/charset.html
regards, tom lane