I want to sort test last name and test2 last name so that the former comes before the latter. My understanding is that each character is compared from left to right until they differ and therefore the characters after those first differing characters do not matter anymore. However, as shown below, test comes before test2 but as soon as I append another character, the order changes. Why does this happen? What collation should I use to get the desired order? Note that converting them to bytea would yield the desired order.
test=# SELECT 'test last name' < 'test2 last name' COLLATE "en_US"; ?column? ---------- f (1 row) test=# SELECT 'test last' < 'test2 last' COLLATE "en_US"; ?column? ---------- f (1 row) test=# SELECT 'test ' < 'test2 ' COLLATE "en_US"; ?column? ---------- t (1 row) test=# SELECT 'test ' < 'test2' COLLATE "en_US"; ?column? ---------- t (1 row) test=# SELECT 'test' < 'test2' COLLATE "en_US"; ?column? ---------- t (1 row) test=# SELECT 'test ' < 'test2' COLLATE "en_US"; ?column? ---------- t (1 row) test=# SELECT 'test ' < 'test2 ' COLLATE "en_US"; ?column? ---------- t (1 row) test=# SELECT 'test ' < 'test2 ' COLLATE "en_US"; ?column? ---------- t (1 row) test=# SELECT 'test ' < 'test2 ' COLLATE "en_US"; ?column? ---------- t (1 row) test=# SELECT 'test ' < 'test2 l' COLLATE "en_US"; ?column? ---------- t (1 row) test=# SELECT 'test ' < 'test2 l' COLLATE "en_US"; ?column? ---------- t (1 row) test=# SELECT 'test l' < 'test2 l' COLLATE "en_US"; ?column? ---------- f (1 row) test=# SELECT 'test l' < 'test2l' COLLATE "en_US"; ?column? ---------- f (1 row) test=# SELECT 'test ' < 'test2l' COLLATE "en_US"; ?column? ---------- t (1 row) test=# SELECT 'test last name'::bytea < 'test2 last name'::bytea; ?column? ---------- t (1 row)