snowflake.snowpark.functions.array_sort

snowflake.snowpark.functions.array_sort(array: Union[Column, str], sort_ascending: Optional[bool] = True, nulls_first: Optional[bool] = False) Column[source] (https://github.com/snowflakedb/snowpark-python/blob/v1.26.0/snowpark-python/src/snowflake/snowpark/functions.py#L4638-L4730)

Returns rows of array column in sorted order. Users can choose the sort order and decide where to keep null elements.

Parameters:
  • array – name of the column or column element which describes the column

  • sort_ascending – Boolean that decides if array elements are sorted in ascending order. Defaults to True.

  • nulls_first – Boolean that decides if SQL null elements will be placed in the beginning of the array. Note that this does not affect JSON null. Defaults to False.

Examples::
Behavior with SQL nulls:
>>> df = session.sql("select array_construct(20, 0, null, 10) as A")
>>> df.select(array_sort(df.a).as_("sorted_a")).show()
---------------
|"SORTED_A"   |
---------------
|[            |
|  0,         |
|  10,        |
|  20,        |
|  undefined  |
|]            |
---------------

>>> df.select(array_sort(df.a, False).as_("sorted_a")).show()
---------------
|"SORTED_A"   |
---------------
|[            |
|  20,        |
|  10,        |
|  0,         |
|  undefined  |
|]            |
---------------

>>> df.select(array_sort(df.a, False, True).as_("sorted_a")).show()
----------------
|"SORTED_A"    |
----------------
|[             |
|  undefined,  |
|  20,         |
|  10,         |
|  0           |
|]             |
----------------
Copy
Behavior with JSON nulls:
>>> df = session.create_dataframe([[[20, 0, None, 10]]], schema=["a"])
>>> df.select(array_sort(df.a, False, False).as_("sorted_a")).show()
--------------
|"SORTED_A"  |
--------------
|[           |
|  null,     |
|  20,       |
|  10,       |
|  0         |
|]           |
--------------

>>> df.select(array_sort(df.a, False, True).as_("sorted_a")).show()
--------------
|"SORTED_A"  |
--------------
|[           |
|  null,     |
|  20,       |
|  10,       |
|  0         |
|]           |
--------------
Copy
Language: English