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]¶

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

When called with no column arguments, sorts by all columns (ORDER BY ALL).

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    |
-------------


>>> # Sort by all columns (ORDER BY ALL) - no columns specified
>>> df.sort().show()
-------------
|"A"  |"B"  |
-------------
|1    |2    |
|1    |4    |
|3    |4    |
-------------


>>> # Sort by all columns (ORDER BY ALL) - no columns specified
>>> df.sort([], ascending=False).show()
-------------
|"A"  |"B"  |
-------------
|3    |4    |
|1    |4    |
|1    |2    |
-------------
Copy
Parameters:
  • *cols – Column names as str, Column objects, or a list of columns to sort by. If no columns are provided, the DataFrame is sorted by all columns in the order they appear (equivalent to ORDER BY ALL in SQL).

  • ascending –

    Sort order specification.

    • When sorting specific columns: A bool, int, or list of bool/int values. True (or 1) for ascending, False (or 0) for descending. If a list is provided, its length must match the number of columns.

    • When sorting all columns (no columns specified): Must be a single bool or int, not a list. Applies the same sort order to all columns.

    • Defaults to True (ascending) when not specified.

Note

The aliases order_by() and orderBy() have the same behavior.