Multi-key ordering
Create a deterministic ranking ordered by several fields with independent ascending or descending directions.
Polars · Module
Sort a Polars dataframe by one or more columns or expressions. Control direction, null placement, threading, and ordering of equal rows.
DataFrame.sort returns a dataframe ordered by one or more column expressions. Supply additional sort keys positionally, choose global or per-column descending and null placement, and optionally enable multithreading or preserve the order of equal rows.
DataFrame.sort(by: IntoExpr | Iterable[IntoExpr], *more_by: IntoExpr, descending: bool | Sequence[bool] = False, nulls_last: bool | Sequence[bool] = False, multithreaded: bool = True, maintain_order: bool = False) → DataFrame| Name | Type | Description |
|---|---|---|
by | IntoExpr | Iterable[IntoExpr] | Column or columns to sort by; accepts expressions and selectors, while strings are parsed as column names. |
more_by | IntoExpr | Additional sort columns supplied positionally. |
descending | bool | Sequence[bool] = False | Sort descending; for multiple keys, use a boolean sequence for per-key direction. |
nulls_last | bool | Sequence[bool] = False | Place nulls last globally or provide per-column boolean control. |
multithreaded | bool = True | Whether to use multiple threads for sorting. |
maintain_order | bool = False | Whether equal elements should retain their existing order. |
DataFrame
by argument supports Polars expressions and selectors, not only literal column names.more_by positional arguments to append secondary and later sort keys.multithreaded flag enables multithreaded sorting when left at its default or set to true.maintain_order controls whether rows tied on all sort keys retain their original relative order.descending must correspond to the sort columns when different directions are required.nulls_last may be one boolean for every key or a sequence that controls each key separately.import polars as pl
df = pl.DataFrame({"group": ["b", "a", "a"], "score": [2, 3, 1]})
result = df.sort(
["group", "score"],
descending=[False, True],
nulls_last=True,
)
print(result)Sorts by group ascending, then score descending, placing nulls after non-null values.
Create a deterministic ranking ordered by several fields with independent ascending or descending directions.
Move missing values after populated values while sorting one or more columns.
Retain input order among rows tied on every sort key.
DataFrame.sort do, and what can by contain?It orders a dataframe using the supplied column names or expressions and returns a DataFrame. Multiple keys can be passed, and strings are interpreted as column names.
Follow-up: How would you apply different directions to multiple keys? Pass a boolean sequence to descending, with one value per sort column.
Use descending=True for descending order. For multiple columns, provide a boolean sequence so each key can have its own direction.
Follow-up: How can null placement vary by key? Supply a boolean sequence to nulls_last, aligned with the sort columns.
multithreaded and maintain_order options?multithreaded=True requests a multithreaded sort, while maintain_order=True preserves the order of rows that compare equal. Both are optional controls.
Follow-up: When would stable ordering matter? Use maintain_order=True when equal sort keys must retain their input order.
polars.DataFrame.sortpolars.Expr.sortpolars.Expr.sort_bypolars.DataFrame