Sort Rows

Shipping
df_sort

Stable sort the rows by one column, ascending or descending, with nulls last

Signature

Inputs

  • dfDataFramerequiredThe frame whose rows are reordered by one key column.

Outputs

  • dfDataFrameThe same rows in sorted order; every column rides the same permutation.

Parameters

KeyTypeDefaultNotes
columnstringColumn to sort by (name or 0-based index). Numeric/date columns sort numerically, text lexicographically; nulls sort last.
orderenumascone of: asc, desc

Description

Sort Rows (df_sort) orders a DataFrame's rows by one key column and reindexes every column by that permutation, so sibling columns ride along and stay aligned. You choose the column (name or index) and the order (ascending or descending).

The comparison follows the column's type: numeric and datetime columns sort numerically, text sorts lexicographically. NULL cells always sort LAST in both directions — the descending flag only reverses the present-vs-present comparison, so missing values never crowd the informative end of the table. The sort is stable: rows with equal keys keep their original relative order, which makes multi-key sorts composable (sort by the secondary key first, then the primary).

Sorting only permutes existing cells, so types, units and null masks are preserved. The node is stateless; an unknown column is a hard error.

Mathematics

Examples

Ascending reorders all columns together

A frame with v = [3, 1, 2] and sibling lbl = ["x", "y", "z"], sorted ascending by v, becomes v = [1, 2, 3], lbl = ["y", "z", "x"] — the label column follows v's permutation.

Nulls last in both directions

A column v = [3, 1, null, 2] sorts ascending to indices [1, 3, 0, 2]1, 2, 3, null, and descending to [0, 3, 1, 2]3, 2, 1, null. The null pins to the end either way.

Applications

  • Ordering a measurement table by time or by an independent variable before plotting or interpolation.
  • Ranking rows by a computed metric (largest error first) with a single descending sort.
  • Building a multi-key ordering by chaining stable sorts (least-significant key first).
  • Grouping equal-key rows contiguously (via a stable sort) ahead of downstream segmentation.

Neat

Null handling is direction-independent: descending only flips the value comparison, so missing cells never migrate to the top of a descending sort.

The sort is stable, so applying df_sort repeatedly on different keys yields a proper lexicographic multi-key order without any dedicated multi-column node.

Numeric and datetime columns share the same f64 key path, so timestamps and measurements sort by true magnitude, not by string form.

Known issues

Text columns sort lexicographically, so numeric-looking strings order by characters ("10" before "9"); convert to a numeric column for value order.

Only single-column sorting is built in; multi-key ordering requires chaining sorts in least-to-most-significant order.

See also

dataframesortrowsstablenull-awarestateless