4

In order to sort a PostgreSQL 14 varchar column with numerical values, I'm using a collation created by

CREATE COLLATION de_pos (LOCALE = 'de-DE-u-kn-true', PROVIDER = 'icu');`

With such a collation ORDER BY results in correct order like this
1.2.10
1.2.20
1.2.100

Without that special collation ORDER BY results in
1.2.10
1.2.100
1.2.20

I want to know, what every part of that collation definition exactly means ?

de-DE => locale for german-germany
u => UTF8 ????
kn => ????
true => ????

4
  • As a side note: You can also sort version numbers in a different way: order by string_to_array(the_column, '.')::int[] but that only works if the values are strictly numeric
    – user330315
    Nov 5, 2021 at 12:34
  • Yes, I know that function and made a test: it works only for two levels (that means for a single dot, 1.10, 1.20, 1.100 etc) but I need an quasi recursive string_to_array due to my values are 1.2.3.10, 1.2.3.20 etc.
    – Dominik
    Nov 6, 2021 at 16:00
  • That will work with something like 1.2.3.20 just as well dbfiddle.uk/…
    – user330315
    Nov 6, 2021 at 16:20
  • wow, thought I've tested that, great ... thanks
    – Dominik
    Nov 7, 2021 at 18:38

2 Answers 2

14

See ICU Collations in the PostgreSQL documentation. This links to the ICU documentation, which - with some indirection - leads to Unicode Locale Identifier, which makes clear that the -u introduces the Unicode Locale Extensions, and kn is one of those extensions. When you look at Collation Settings, you'll find kn configures numeric ordering. The true is the configuration of that option (meaning, numeric ordering is on):

If set to on, any sequence of Decimal Digits (General_Category = Nd in the [UAX44]) is sorted at a primary level with its numeric value. For example, "A-21" < "A-123". The computed primary weights are all at the start of the digit reordering group. Thus with an untailored UCA table, "a$" < "a0" < "a2" < "a12" < "a⓪" < "aa".

This is sometimes called “natural sort order”.

In other words, de-DE-u-kn-true is:

  • de: language German
  • DE: region Germany
  • u: what follows are Unicode Locale Extension
  • kn: Unicode Locale Extension numeric ordering
  • true: value of kn, meaning numeric ordering is on
5
  • Thank you for your explanation. I'm already stumbled upon the unicode documentation you mentioned. But I didn't find any single occurrence of 'kn' withih the whole document. That's why I'm asking for.
    – Dominik
    Nov 6, 2021 at 16:07
  • 1
    @Dominik It's listed in the table at unicode.org/reports/tr35/tr35-collation.html#Collation_Settings Nov 6, 2021 at 16:08
  • Thank you, I've create a request for extend the PostgreSQL docs with this link.
    – Dominik
    Nov 7, 2021 at 18:47
  • @Dominik, was it added to PostgreSQL docs ? What the request URL you created ? Nov 26, 2022 at 22:14
  • as of right now pg docs include the link in ref to "Unicode Technical Standard #35" May 15 at 22:27
1

You can customize your collation's sorting behavior by specifying attribute names. In this case, kn-true stands for natural sorting order or numeric sort order that sorts numbers based on their numeric value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.