polars.Expr.sort_by#

Expr.sort_by(by: polars.internals.expr.expr.Expr | str | list[polars.internals.expr.expr.Expr | str], reverse: bool | list[bool] = False) Expr[source]#

Sort this column by the ordering of another column, or multiple other columns.

In projection/ selection context the whole column is sorted. If used in a groupby context, the groups are sorted.

Parameters:
by

The column(s) used for sorting.

reverse

False -> order from small to large. True -> order from large to small.

Examples

>>> df = pl.DataFrame(
...     {
...         "group": [
...             "one",
...             "one",
...             "one",
...             "two",
...             "two",
...             "two",
...         ],
...         "value": [1, 98, 2, 3, 99, 4],
...     }
... )
>>> df.select(pl.col("group").sort_by("value"))
shape: (6, 1)
┌───────┐
│ group │
│ ---   │
│ str   │
╞═══════╡
│ one   │
├╌╌╌╌╌╌╌┤
│ one   │
├╌╌╌╌╌╌╌┤
│ two   │
├╌╌╌╌╌╌╌┤
│ two   │
├╌╌╌╌╌╌╌┤
│ one   │
├╌╌╌╌╌╌╌┤
│ two   │
└───────┘