pyspark.sql.functions.sort_array(col: ColumnOrName, asc: bool = True) → pyspark.sql.column.Column[source]

Collection function: sorts the input array in ascending or descending order according to the natural ordering of the array elements. Null elements will be placed at the beginning of the returned array in ascending order or at the end of the returned array in descending order.

New in version 1.5.0.

Changed in version 3.4.0: Supports Spark Connect.

colColumn or str

name of column or expression

ascbool, optional

whether to sort in ascending or descending order. If asc is True (default) then ascending and if False then descending.


sorted array.


>>> df = spark.createDataFrame([([2, 1, None, 3],),([1],),([],)], ['data'])
[Row(r=[None, 1, 2, 3]), Row(r=[1]), Row(r=[])]
>>>, asc=False).alias('r')).collect()
[Row(r=[3, 2, 1, None]), Row(r=[1]), Row(r=[])]