I have a column in my table that are codes. They are in the format of
AAA-XXXX-YYY
where
AAA
is AlphabeticalXXX
is NumericYYY
is Numeric
I want to use an alphabetical sort on AAA
, then numeric sort on XXXX
, then numeric sort on YYY
for my default_scope so when I index them they are sorted like that.
If I did an alphanumeric sort on the column I would end up with the following:
AAA-1000-1
AAA-1000-111
AAA-1000-2
BB-1000-12
BBB-1000-1
BCA-1000-1
C-1000-12
but 111 is bigger than 2 using a numeric sort.
EDIT:AAA is not fixed in length. So it could be "AA" or "BBBB" or "CCC". There has been mention of using SQL SUBSTRING but the position is not always set.
EDIT2: I probably should mention this sort needs to be done on a default_scope so when I access Object.all they are by default sorted this way.
Thanks for your help!