Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Polars · Module

df.sort

Sort a Polars dataframe by one or more columns or expressions. Control direction, null placement, threading, and ordering of equal rows.

How it works

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.

Syntax

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

Parameters

NameTypeDescription
byIntoExpr | Iterable[IntoExpr]Column or columns to sort by; accepts expressions and selectors, while strings are parsed as column names.
more_byIntoExprAdditional sort columns supplied positionally.
descendingbool | Sequence[bool] = FalseSort descending; for multiple keys, use a boolean sequence for per-key direction.
nulls_lastbool | Sequence[bool] = FalsePlace nulls last globally or provide per-column boolean control.
multithreadedbool = TrueWhether to use multiple threads for sorting.
maintain_orderbool = FalseWhether equal elements should retain their existing order.

Returns

DataFrame

Key points

  • The method produces a dataframe sorted by the selected keys.
  • The by argument supports Polars expressions and selectors, not only literal column names.
  • A string passed as a sort key is treated as a column reference.
  • Use more_by positional arguments to append secondary and later sort keys.
  • The 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.

Common mistakes

  • A sequence for 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.

Example: Sort by multiple columns

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.

When to use it

Multi-key ordering

Create a deterministic ranking ordered by several fields with independent ascending or descending directions.

Null placement

Move missing values after populated values while sorting one or more columns.

Stable tie handling

Retain input order among rows tied on every sort key.

Interview questions

QWhat does 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.

QHow do you control sort direction?

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.

QWhat are the 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.sort
  • polars.Expr.sort
  • polars.Expr.sort_by
  • polars.DataFrame

Source: polars.DataFrame.sort — Polars documentation. Checked 2026-10-15.