Skip to main content

asc_nulls_last

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

Syntax

Python
from pyspark.databricks.sql import functions as dbf

dbf.asc_nulls_last(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.

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

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

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