Collation order in Rails with Postgresql

513 views
Skip to first unread message

Donald Ziesig

unread,
Mar 11, 2016, 7:49:53 PM3/11/16
to rubyonrails-talk
Hi All!

I am trying to get a rails postgres table ordered by the exact values of
the ascii characters such that:

<none>
Alpha
Bravo
Charlie
...

Unfortunately, even when I try to escape the < characters and enter them
directly in the Postgresql table, I get:

Alpha
Bravo
*
*
*
Lima
Mike
<none>
November
Oscar

I tried @@@@ in place of <none> and it is positioned as I expect, but
@none@ is handled the same as <none>.

I've searched the web, but there are too many different solutions. It
will take me all day and night to try them out,

Can anyone point me in the right direction?

Thanks,

Don Ziesig

Colin Law

unread,
Mar 12, 2016, 3:40:25 AM3/12/16
to Ruby on Rails: Talk
possibly
model.all.order(:name)
where name is the column you want to sort on.

Colin

nanaya

unread,
Mar 12, 2016, 3:55:30 AM3/12/16
to rubyonra...@googlegroups.com
Hi,
What you want is the C collation[1]. You can either:

- have it during query (`MyModel.order('name COLLATE "C"')`),

- create the column with C collation (`t.string :my_column, :collation
=> "C"` iirc),

- or set default database collation (check either `CREATE DATABASE`
documentation[2] or `initdb`[3]).


[1] http://www.postgresql.org/docs/current/static/collation.html

[2]
http://www.postgresql.org/docs/current/static/sql-createdatabase.html

[3] http://www.postgresql.org/docs/current/static/app-initdb.html
Reply all
Reply to author
Forward
0 new messages