How to sort text in PostgreSQL in Latvian alphabetic order, so any letter without accent always comes before the letter with accent?
For example this
select * from (
select 'bā' as name
union select 'āa'
union select 'ac'
union select 'Āb'
union select 'Ā'
) t
order by name COLLATE "lv-x-icu"
returns Ā āa Āb ac bā
but the expected result is ac Ā āa Āb bā
I have tried to create some custom collations but none of them returned the expected result.
2
Answers
You can use
CASE
inORDER BY
, and regular expression for "not ASCII", so the row with only ASCII characters ‘ac’ appears first (sorted by 0).the rows with non-ASCII characters appear last (sorted by 1).Demo here
If you have PostgreSQL built with ICU support, you can create a collation that sorts upper case letters first:
The notice show you the new syntax. I used the old syntax, in case you are using an old ICU library.