From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Jimmy Thrasher <jimmy@jimmythrasher.com>
To: pgsql-general@postgresql.org
Subject: Re: Unexpected behavior sorting strings
Date: Wed, 8 Apr 2020 08:49:34 -0700
Message-ID: <a0bfe027-41e9-5222-d090-7fd7abb006aa@aklaver.com> (raw)
In-Reply-To: <410dddd6-bc66-48ca-ab9d-13889d94b62c@www.fastmail.com>
References: <410dddd6-bc66-48ca-ab9d-13889d94b62c@www.fastmail.com>
On 4/8/20 7:35 AM, Jimmy Thrasher wrote:
> I'm seeing some unexpected behavior when sorting some strings, and it indicates I don't fully understand how postgresql string sorting works.
>
> As I understand it, postgresql sorts strings roughly like strcmp does: character by character based on encoding value.
>
> In particular, I'm seeing the following. I would expect "< S" to come first, because "<" (0x3c) is less than ">" (0x3e).
>
> ```
> supercatdev=# select unnest(array['> N', '< S']) as s order by s;
> s
> -----
> > N
> < S
> (2 rows)
> ```
>
> I've broken this down further:
> ```
> supercatdev=# select '> N' < '< S';
> ?column?
> ----------
> t
> (1 row)
> ```
>
> Am I missing something about how sorting works?
I believe you are looking for 'C' collation:
test=# select unnest(array[('> N' collate "C") , ('< S' COLLATE "C")])
as s order by s;
s
-----
< S
> N
(2 rows)
For more information see:
https://www.postgresql.org/docs/12/collation.html>
> Metadata:
> - postgresql 9.5.19, running on Ubuntu 16LTS
> - encoding, collate, and ctype are all UTF8 or en_US.UTF-8, as appropriate
>
> Thanks!
>
> Jimmy
>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: adrian.klaver@aklaver.com, jimmy@jimmythrasher.com
Subject: Re: Unexpected behavior sorting strings
In-Reply-To: <a0bfe027-41e9-5222-d090-7fd7abb006aa@aklaver.com>
References: <410dddd6-bc66-48ca-ab9d-13889d94b62c@www.fastmail.com> <a0bfe027-41e9-5222-d090-7fd7abb006aa@aklaver.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox