I have table with column :name
.
I want ordering :name
alphabetically.
Use Item.order(:name)
and get "item 14", "item 15", "item 16", "item 3", "item 4"
. But I want "item 3", "item 4", "item 14", "item 15", "item 16"
How to achieve this?
I have table with column :name
.
I want ordering :name
alphabetically.
Use Item.order(:name)
and get "item 14", "item 15", "item 16", "item 3", "item 4"
. But I want "item 3", "item 4", "item 14", "item 15", "item 16"
How to achieve this?
This seems to work
Item.order("LENGTH(name)", :name).pluck(:name)
You can check this out: MySQL 'Order By' - sorting alphanumeric correctly
Have you tried:
Item.order('CAST(name AS DECIMAL) ASC')
Just a guess, but could work... good luck!
Thanks @razvans for the direction of the search. I found solution enter link description here
I have:
id | name
----+----------
2 | item 2
3 | item 12
1 | item 3
4 | item 17
5 | df38
6 | aaa
7 | a13v
8 | a13a
9 | item 00
(9 rows)
And with this code:
select name from table_name order by left(name, 1), substring(name, '\d+')::int NULLS FIRST, name;
I got:
name
----------
aaa
a13a
a13v
df38
item 00
item 2
item 3
item 12
item 17
(9 rows)
This solution is on ruby on rails:
Item.order("left(name, 1), substring(name, '\\d+')::int NULLS FIRST, name").pluck(:name)
That's what I need!