pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedUnexpected behavior sorting strings
4+ messages / 3 participants
[nested] [flat]
* Unexpected behavior sorting strings
@ 2020-04-08 14:35 Jimmy Thrasher <jimmy@jimmythrasher.com>
0 siblings, 2 replies; 4+ messages in thread
From: Jimmy Thrasher @ 2020-04-08 14:35 UTC (permalink / raw)
To: pgsql-general
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?
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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Unexpected behavior sorting strings
@ 2020-04-08 15:47 Tom Lane <tgl@sss.pgh.pa.us>
parent: Jimmy Thrasher <jimmy@jimmythrasher.com>
1 sibling, 0 replies; 4+ messages in thread
From: Tom Lane @ 2020-04-08 15:47 UTC (permalink / raw)
To: Jimmy Thrasher <jimmy@jimmythrasher.com>; +Cc: pgsql-general
"Jimmy Thrasher" <jimmy@jimmythrasher.com> writes:
> As I understand it, postgresql sorts strings roughly like strcmp does: character by character based on encoding value.
Only if you're using C locale. Other locales such as en_US have
completely different rules, which most hackers tend to find pretty
unintelligible and inconsistent :-(. In your example, I think
the first-pass sort is on just the letters, and only if those are
the same will it consider the punctuation.
regards, tom lane
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Unexpected behavior sorting strings
@ 2020-04-08 15:49 Adrian Klaver <adrian.klaver@aklaver.com>
parent: Jimmy Thrasher <jimmy@jimmythrasher.com>
1 sibling, 1 reply; 4+ messages in thread
From: Adrian Klaver @ 2020-04-08 15:49 UTC (permalink / raw)
To: Jimmy Thrasher <jimmy@jimmythrasher.com>; pgsql-general
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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Unexpected behavior sorting strings
@ 2020-04-08 16:08 Jimmy Thrasher <jimmy@jimmythrasher.com>
parent: Adrian Klaver <adrian.klaver@aklaver.com>
0 siblings, 0 replies; 4+ messages in thread
From: Jimmy Thrasher @ 2020-04-08 16:08 UTC (permalink / raw)
To: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general
Many thanks! That clarifies things well.
Jimmy
On Wed, Apr 8, 2020, at 11:49 AM, Adrian Klaver wrote:
> On 4/8/20 7:35 AM, Jimmy Thrasher wrote:
<snip>
> > 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
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2020-04-08 16:08 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-04-08 14:35 Unexpected behavior sorting strings Jimmy Thrasher <jimmy@jimmythrasher.com>
2020-04-08 15:47 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-04-08 15:49 ` Adrian Klaver <adrian.klaver@aklaver.com>
2020-04-08 16:08 ` Jimmy Thrasher <jimmy@jimmythrasher.com>
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