I have an array of strings as one of my columns, and I want to sort the result by the first element of the array. This is what I have tried:
SELECT * FROM items ORDER BY aliases[0];
This did not work. How may this be accomplished?
Arrays in Postgres are indexed beginning at position 1, not 0. From the documentation:
By default PostgreSQL uses a one-based numbering convention for arrays, that is, an array of n elements starts with array[1] and ends with array[n].
With this in mind, try the following query:
SELECT * FROM items ORDER BY aliases[1];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With