sort_array function

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime

Returns the array in expr in sorted order.


sort_array(expr [, ascendingOrder] )


  • expr: An ARRAY expression of sortable elements.

  • ascendingOrder: An optional BOOLEAN expression defaulting to true.


The result type matches expr.

Sorts the input array in ascending or descending order according to the natural ordering of the array elements. NULL elements are placed at the beginning of the returned array in ascending order or at the end of the returned array in descending order.


> SELECT sort_array(array('b', 'd', NULL, 'c', 'a'), true);