← Back to Upcase

Alphanumeric Order | ActiveRecord Queries


(Alex Bush) #1

I’m trying to give my iOS app some ordered JSON data and I need to have a custom order(UILocalized​Indexed​Collation order really but this is a topic for another discussion)

One of my test queries looks like this:
y fsts = Festival.all(:order => '0+name ASC, name ASC').map(&:name)

The output I get looks correct:

- AddictedToRadio.com South Beach Tour lll
- AddictedToRadio.com South Beach Tour lll 2
- Electric Beach Festival
- Flashback!
- Winter Music Conference
- WMC 2014 DJ Spin-Off Beatmatching
- WMC 2014 Exhibiting
- 14th Edition RE-UNION

but I’m not 100% sure that it is correct…

The way I want the output data to be ordered is

A
B
C

1
2
3

!

if it makes sense…

Am I on the right track but need to adjust it or this SQL query I have would be sufficient enough?


(Geoff Harcourt) #2

Google and Stack Overflow aren’t very promising here. You could construct a big case statement for your order clause (and then hopefully be able to reuse it elsewhere).

Here’s one of the more relevant SO posts:
http://stackoverflow.com/questions/11417975/alphanumeric-sorting-with-postgresql


(Robert Lee-Cann) #3

How about using Ruby’s sort method. It accepts an optional block, which allows you to customise how Ruby sorts the results.

I found this on StackOverflow, which may be of help: http://stackoverflow.com/questions/5480703/how-to-sort-an-alphanumeric-array-in-ruby