snowflake.snowpark.DataFrame.order_by

DataFrame.order_by(*cols: Union[Column, str, Iterable[Union[Column, str]]], ascending: Optional[Union[bool, int, List[Union[bool, int]]]] = None) DataFrame[source] (https://github.com/snowflakedb/snowpark-python/blob/v1.26.0/snowpark-python/src/snowflake/snowpark/dataframe.py#L1696-L1833)

Sorts a DataFrame by the specified expressions (similar to ORDER BY in SQL).

Examples:

>>> from snowflake.snowpark.functions import col

>>> df = session.create_dataframe([[1, 2], [3, 4], [1, 4]], schema=["A", "B"])
>>> df.sort(col("A"), col("B").asc()).show()
-------------
|"A"  |"B"  |
-------------
|1    |2    |
|1    |4    |
|3    |4    |
-------------


>>> df.sort(col("a"), ascending=False).show()
-------------
|"A"  |"B"  |
-------------
|3    |4    |
|1    |2    |
|1    |4    |
-------------


>>> # The values from the list overwrite the column ordering.
>>> df.sort(["a", col("b").desc()], ascending=[1, 1]).show()
-------------
|"A"  |"B"  |
-------------
|1    |2    |
|1    |4    |
|3    |4    |
-------------
Copy
Parameters:
  • *cols – A column name as str or Column, or a list of columns to sort by.

  • ascending – A bool or a list of bool for sorting the DataFrame, where True sorts a column in ascending order and False sorts a column in descending order . If you specify a list of multiple sort orders, the length of the list must equal the number of columns.

Language: English