ORDER BY key->>1::int, key->>2::int, key->>3::int
There is no easy way, presently, to convert from a json array to a PostgreSQL array. If you do that I believe that those sort based upon the values and not lexically.
SELECT *
FROM ( VALUES (ARRAY[2014]::int[], ARRAY[2014,2]::int[], ARRAY[2015]::int[]) ) vals (v)
ORDER BY v;
David J.
I spent a couple minutes goofing off on this question - this isn't exactly right and is UGLY, but maybe helps a bit with some ideas (the virtual table is needless, but my sql is rusty):
SELECT key, to_number(key#>>'{0}','9999') as order1, to_number(key#>>'{1}','9') as order2 FROM FOO
order by order1, order2
"[2014, 2, 4]";2014;2
"[2014, 2, 15]";2014;2
"[2014, 2, 4]";2014;2
"[2014, 2]";2014;2
"[2014, 2, 3]";2014;2
"[2014, 3]";2014;3
"[2014, 3, 13]";2014;3
"[2014]";2014;
"[2015]";2015;
"[2016]";2016;
2014 is coming after all the elements that have a value associated with 2014 - probably you could solve this with a coalesce, but I'm out of time messing with it. I hope it's helpful!
Steve
In my case the array may be much longer and contain not just numbers, but also strings.
It may be easier to patch RDBMS sources. Where is this sort performed?