You are viewing documentation about an older version (1.11.1). View latest version

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]

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

Make sure BCR 2023_05 Bundle is enabled before using this function.

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