Order SELECT by the first element of an array?

I have an array of rows as one of my columns, and I want to sort the result by the first element of the array. This is what I tried:

SELECT * FROM items ORDER BY aliases[0];

This did not work. How can this be achieved?

+4
source share
1 answer

Arrays in Postgres are indexed starting at position 1, not 0. From the documentation :

By default, PostgreSQL uses a one-time 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];
+2
source

Source: https://habr.com/ru/post/1689547/


All Articles