pyspark.sql.functions.
sort_array
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.
Column
name of column or expression
whether to sort in ascending or descending order. If asc is True (default) then ascending and if False then descending.
sorted array.
Examples
>>> df = spark.createDataFrame([([2, 1, None, 3],),([1],),([],)], ['data']) >>> df.select(sort_array(df.data).alias('r')).collect() [Row(r=[None, 1, 2, 3]), Row(r=[1]), Row(r=[])] >>> df.select(sort_array(df.data, asc=False).alias('r')).collect() [Row(r=[3, 2, 1, None]), Row(r=[1]), Row(r=[])]