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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_sort(array, descending, nulls_first)

Parameters

ParameterDescription
array An array value.
descending optional Sort in reverse order. Default: false
nulls_first optional Put NULLs first.

Return value

A sorted array.

Examples

Sort an array
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.

Text uses the collation
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

VersionChange
PostgreSQL 18Added

Was this page helpful?