I use PostgreSQL 9.3.3 and I have a table with one column named as title (character varying(50)).
When I have executed the following query:
select * from test
order by title asc
I got the following results:
#
A
#Example
Why "#Example" is in the last position? In my opinion "#Example" should be in the second position.
Sort behaviour for text (including
char
andvarchar
as well as thetext
type) depends on the current collation of your locale.See previous closely related questions:
If you want to do a simplistic sort by ASCII value, rather than a properly localized sort following your local language rules, you can use the
COLLATE
clauseor change the database collation globally (requires dump and reload, or full reindex). On my Fedora 19 Linux system, I get the following results:
PostgreSQL uses your operating system's collation support, so it's possible for results to vary slightly from host OS to host OS. In particular, at least some versions of Mac OS X have significantly broken unicode collation handling.
It seems, that when sorting Oracle as well as Postgres just ignore non alpha numeric chars, e.g.
returns (look: that DBMS doesn't pay any attention on prefix before 'A'..'E')
In your case, what Postgres actually sorts is
''
,'A'
and'Example'
If you put
'#'
in the middle od the string, the behaviour will be the same:returns (
#
ignored, and so'AB', 'AC', 'AD'
and'AE'
actually compared)To change the comparison rules you should use collation, e.g.
returns (as it required in your case)