PostgreSQL 18 reference · Array Functions
PostgreSQL ARRAY_SORT() Function
Return a copy of an array with its elements sorted, ascending by default. Added in PostgreSQL 18.
PostgreSQL 18 Since 18
Syntax
array_sort(array, descending, nulls_first)
Parameters
| Parameter | Description |
|---|---|
array |
An array value. |
descending optional |
Sort in reverse order. Default: false |
nulls_first optional |
Put NULLs first. |
Return value
A sorted array.
Examples
SELECT array_sort(ARRAY[3, 1, 2]) AS asc_order, array_sort(ARRAY[3, 1, 2], true) AS desc_order;
Output
asc_order | desc_order
-----------+------------
{1,2,3} | {3,2,1}
(1 row)Run this on your computer — see “Get Started” for setup.
SELECT array_sort(ARRAY['pear', 'Apple', 'fig']) AS words;
Output
words
------------------
{Apple,fig,pear}
(1 row)Run this on your computer — see “Get Started” for setup.
Version history
| Version | Change |
|---|---|
| PostgreSQL 18 | Added |
Related
ARRAY_REVERSE()· Return a copy of an array with its elements in reverse order. Added in PostgreSQL 18.ARRAY_AGG()· Collect a group's values, including NULLs, into an array, optionally in a given order.- SQL String Functions · tutorial