Sort Expr by order of others
Description
Sort this column by the ordering of another column, or multiple other columns. If used in a groupby context, the groups are sorted.
Usage
<Expr>$sort_by(
by,
...,
descending = FALSE,
nulls_last = FALSE,
multithreaded = TRUE,
maintain_order = FALSE
)
Arguments
by
|
One expression or a list of expressions and/or strings (interpreted as column names). |
…
|
Ignored. |
descending
|
A logical. If TRUE , sort in descending order.
|
nulls_last
|
A logical. If TRUE , place null values last
insead of first.
|
multithreaded
|
A logical. If TRUE , sort using multiple threads.
|
maintain_order
|
A logical to indicate whether the order should be maintained if elements are equal. |
Value
Expr
Examples
library("polars")
df = pl$DataFrame(
group = c("a", "a", "a", "b", "b", "b"),
value1 = c(98, 1, 3, 2, 99, 100),
value2 = c("d", "f", "b", "e", "c", "a")
)
# by one column/expression
df$with_columns(
sorted = pl$col("group")$sort_by("value1")
)
#> shape: (6, 4)
#> ┌───────┬────────┬────────┬────────┐
#> │ group ┆ value1 ┆ value2 ┆ sorted │
#> │ --- ┆ --- ┆ --- ┆ --- │
#> │ str ┆ f64 ┆ str ┆ str │
#> ╞═══════╪════════╪════════╪════════╡
#> │ a ┆ 98.0 ┆ d ┆ a │
#> │ a ┆ 1.0 ┆ f ┆ b │
#> │ a ┆ 3.0 ┆ b ┆ a │
#> │ b ┆ 2.0 ┆ e ┆ a │
#> │ b ┆ 99.0 ┆ c ┆ b │
#> │ b ┆ 100.0 ┆ a ┆ b │
#> └───────┴────────┴────────┴────────┘
# by two columns/expressions
df$with_columns(
sorted = pl$col("group")$sort_by(
list("value2", pl$col("value1")),
descending = c(TRUE, FALSE)
)
)
#> shape: (6, 4)
#> ┌───────┬────────┬────────┬────────┐
#> │ group ┆ value1 ┆ value2 ┆ sorted │
#> │ --- ┆ --- ┆ --- ┆ --- │
#> │ str ┆ f64 ┆ str ┆ str │
#> ╞═══════╪════════╪════════╪════════╡
#> │ a ┆ 98.0 ┆ d ┆ a │
#> │ a ┆ 1.0 ┆ f ┆ b │
#> │ a ┆ 3.0 ┆ b ┆ a │
#> │ b ┆ 2.0 ┆ e ┆ b │
#> │ b ┆ 99.0 ┆ c ┆ a │
#> │ b ┆ 100.0 ┆ a ┆ b │
#> └───────┴────────┴────────┴────────┘
# by some expression
df$with_columns(
sorted = pl$col("group")$sort_by(pl$col("value1")$sort(descending = TRUE))
)
#> shape: (6, 4)
#> ┌───────┬────────┬────────┬────────┐
#> │ group ┆ value1 ┆ value2 ┆ sorted │
#> │ --- ┆ --- ┆ --- ┆ --- │
#> │ str ┆ f64 ┆ str ┆ str │
#> ╞═══════╪════════╪════════╪════════╡
#> │ a ┆ 98.0 ┆ d ┆ b │
#> │ a ┆ 1.0 ┆ f ┆ b │
#> │ a ┆ 3.0 ┆ b ┆ b │
#> │ b ┆ 2.0 ┆ e ┆ a │
#> │ b ┆ 99.0 ┆ c ┆ a │
#> │ b ┆ 100.0 ┆ a ┆ a │
#> └───────┴────────┴────────┴────────┘