pyspark.sql.functions.asc

pyspark.sql.functions.asc(col: ColumnOrName) → pyspark.sql.column.Column[source]

Returns a sort expression based on the ascending order of the given column name.

New in version 1.3.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
colColumn or str

target column to sort by in the ascending order.

Returns
Column

the column specifying the order.

Examples

Sort by the column ‘id’ in the descending order.

>>> df = spark.range(5)
>>> df = df.sort(desc("id"))
>>> df.show()
+---+
| id|
+---+
|  4|
|  3|
|  2|
|  1|
|  0|
+---+

Sort by the column ‘id’ in the ascending order.

>>> df.orderBy(asc("id")).show()
+---+
| id|
+---+
|  0|
|  1|
|  2|
|  3|
|  4|
+---+