Share via


asc_nulls_first

Returns a sort expression based on the ascending order of the given column name, and null values return before non-null values. Supports Spark Connect.

Syntax

from pyspark.databricks.sql import functions as dbf

dbf.asc_nulls_first(col=<col>)

Parameters

Parameter Type Description
col pyspark.sql.Column or str Target column to sort by in the ascending order.

Returns

pyspark.sql.Column: the column specifying the order.

Examples

Example 1: Sorting a DataFrame with null values in ascending order.

from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(1, "Bob"), (0, None), (2, "Alice")], ["age", "name"])
df.sort(dbf.asc_nulls_first(df.name)).show()
+---+-----+
|age| name|
+---+-----+
|  0| NULL|
|  2|Alice|
|  1|  Bob|
+---+-----+

Example 2: Sorting a DataFrame with null values in ascending order using column name string.

from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(1, "Bob"), (0, None), (2, "Alice")], ["age", "name"])
df.sort(dbf.asc_nulls_first("name")).show()
+---+-----+
|age| name|
+---+-----+
|  0| NULL|
|  2|Alice|
|  1|  Bob|
+---+-----+