Shape / rows · All operations

Sort rows

Order rows.

Status

In arcform

Runs as a SQL step

arcform carries it as a sql: step today.

Shape of the result
one row out per row in, with columns added or changed
Level
one clause of SQL

The DuckDB step

Order rows, nulls last

It reads the orders table the proofs share. Create it first:

SQL
CREATE TABLE orders AS SELECT * FROM (VALUES
  (1,  'Alice Ng',      'north', DATE '2026-01-03', 120.00, 'widget', 'a|b',   '{"channel":"web","items":[1,2]}'),
  (2,  'alice ng',      'north', DATE '2026-01-05',  80.50, 'gadget', 'b',     '{"channel":"shop","items":[3]}'),
  (3,  'Bob  Okafor',   'south', DATE '2026-01-05', 300.00, 'widget', 'a|c',   '{"channel":"web","items":[4,5,6]}'),
  (4,  'Carla Díaz',    'east',  DATE '2026-01-09',  NULL,  'gizmo',  NULL,    '{"channel":"web","items":[]}'),
  (5,  'Bob Okafor',    'south', DATE '2026-01-12',  45.25, 'gadget', 'c',     '{"channel":"phone","items":[7]}'),
  (6,  'Dev Patel',     'west',  DATE '2026-01-12', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (7,  'Carla Diaz',    'east',  DATE '2026-01-20',  99.99, 'gizmo',  'b|c',   '{"channel":"shop","items":[10]}'),
  (8,  'Dev Patel',     'west',  DATE '2026-02-02', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (9,  'Eve Larsen',    NULL,    DATE '2026-02-03',  15.00, 'gadget', NULL,    '{"channel":"web","items":[11]}'),
  (10, 'Alice Ng',      'north', DATE '2026-02-10', 220.00, 'gizmo',  'a|b|c', '{"channel":"phone","items":[12,13]}')
) t(order_id, customer, region, order_date, amount, product, tags, meta);

Then run the statement:

SQL
CREATE TEMP TABLE result AS
SELECT row_number() OVER () AS pos, * FROM (SELECT * FROM orders ORDER BY amount DESC NULLS LAST, order_date);
SELECT * FROM result;
SELECT CASE WHEN ((SELECT list(order_id ORDER BY pos) = [6, 8, 3, 10, 1, 7, 2, 5, 9, 4] FROM result)) THEN 'as expected' ELSE error('the rows are not in descending amount with the null last') END AS verdict;
  • It needs nothing installed beyond DuckDB itself.
  • The statement checks its own result and raises an error when it is wrong: “the rows are not in descending amount with the null last”.
  • It passed on DuckDB v2.0.0 (Cyanoptera) d54cbbe3df.

Variants

Order rows by columns

Order the rows by the values of one or more columns or expressions, each ascending or descending, with nulls first or last.

Move a row by hand

Move one row up, down, or to the top or bottom of the table.

Reverse row order

Turn the current row order upside down without comparing any values.

Shuffle rows

Put the rows into a random order.

What other tools call it

Meridian's classification; an independent blind rating agreed on 86.2% of 312 rows (exact class, version 2).

Alteryx Designer (1)
  • Sortread 2026-09-27

    Order records by the values of one or more fields

Apache DataFusion (4)
  • ORDER BYread 2026-09-27

    Pipe: sort rows, BigQuery-style |> syntax

  • Sorting (ORDER BY)read 2026-09-27

    Order result rows by expressions, aliases or ordinal positions

  • sortread 2026-09-27

    DataFrame method: Sort by sort expressions

  • sort_byread 2026-09-27

    DataFrame method: Sort by expressions in default direction

Apache Hop (1)
  • Sort Rowsread 2026-09-27

    Sort rows by fields ascending or descending, spilling to disk, optionally unique

Azure Data Factory (2)
  • Sortread 2026-09-27

    Sort the rows of the stream

  • Sortingread 2026-09-27

    Table.Sort

DuckDB (1)
Duckle (1)
  • Sortread 2026-09-25

    order rows by keys with direction and null placement

EasyMorph (1)
  • Sortread 2026-09-27

    Order rows by one or more columns

Ibis (2)
  • Sortread 2026-09-27

    Order the rows of a table by a list of sort keys

  • SortKeyread 2026-09-27

    One sort key: expression, ascending or descending, nulls first or last

KNIME Analytics Platform (3)
  • DB Sorterread 2026-09-27

    In-database: order rows by chosen columns

  • Shuffleread 2026-09-27

    Put rows in random order

  • Sorterread 2026-09-27

    Order rows by one or more columns

OpenRefine (2)
  • Make the current temporary sort the stored row order and renumber the rows

  • Sortread 2026-09-27

    Temporarily order rows by a column as text, number, date or boolean, placing errors and blanks

Pentaho Data Integration (1)
  • Sort rowsread 2026-09-27

    Sort rows by field values, ascending or descending

Polars (4)
Power Query M (2)
  • Table.ReverseRowsread 2026-09-27

    Reverse the row order

  • Table.Sortread 2026-09-27

    Order rows by one or more columns, each ascending or descending, or by a comparer

RapidMiner Studio (5)
  • Shuffleread 2026-09-27

    Put rows into random order

  • Sortread 2026-09-27

    Order rows by one or more columns, ascending or descending

  • Sort (In Database)read 2026-09-27

    Order rows by columns, executed in the database

  • Sort (Multiple)read 2026-09-27

    Order rows by several columns, each ascending or descending

  • Sort by Pareto Rankread 2026-09-27

    Order rows by Pareto dominance over several columns, non-dominated rows first

SQLMesh (1)
  • @ORDER_BYread 2026-09-27

    Include an ORDER BY clause only when the argument is true

Tableau Prep (1)
  • { ORDERBY }read 2026-09-27

    Clause setting the row order an analytic function is computed in

Talend (3)
  • tExternalSortOutputread 2026-09-27

    Sort rows on one or more columns with an external sort application and pass them on

  • tExternalSortRowread 2026-09-27

    Sort rows on one or more columns with an external sort application

  • tSortRowread 2026-09-27

    Sort rows on one or more columns by sort type and order

Trifacta (2)
  • Sort Transformread 2026-09-27

    Order rows by the values of one or more columns

  • Sort rowsread 2026-09-27

    Sort rows by the values of one or more columns

VisiData (18)
  • slide-bottomread 2026-09-23

    Move current row to the bottom of the sheet

  • slide-downread 2026-09-23

    Move current row one position down

  • slide-down-nread 2026-09-23

    Move current row N positions down

  • slide-topread 2026-09-23

    Move current row to the top of the sheet

  • slide-upread 2026-09-23

    Move current row one position up

  • slide-up-nread 2026-09-23

    Move current row N positions up

  • sort-ascread 2026-09-23

    Sort ascending by current column, replacing existing sort criteria

  • sort-asc-addread 2026-09-23

    Sort ascending by current column, added to existing sort criteria

  • sort-asc-changeread 2026-09-23

    Sort ascending by current column, keeping higher-priority sort criteria

  • sort-descread 2026-09-23

    Sort descending by current column, replacing existing sort criteria

  • sort-desc-addread 2026-09-23

    Sort descending by current column, added to existing sort criteria

  • sort-desc-changeread 2026-09-23

    Sort descending by current column, keeping higher-priority sort criteria

  • sort-keys-ascread 2026-09-23

    Sort ascending by all key columns, replacing existing sort criteria

  • sort-keys-asc-addread 2026-09-23

    Sort ascending by all key columns, added to existing sort criteria

  • sort-keys-descread 2026-09-23

    Sort descending by all key columns, replacing existing sort criteria

  • sort-keys-desc-addread 2026-09-23

    Sort descending by all key columns, added to existing sort criteria

  • sort-selected-ascread 2026-09-23

    (experimental) Sort the selected rows ascending by current column, in place

  • sort-selected-descread 2026-09-23

    (experimental) Sort the selected rows descending by current column, in place

Background

Sorting orders data in an increasing or decreasing manner according to some linear relationship among the data items.[1] In SQL, an ORDER BY clause makes a SELECT statement return its rows sorted by the values of one or more columns.[2] Without an ORDER BY clause, a relational database system may return the rows in any order.[2]

When to use it

The most common uses of sorted sequences are making lookup or search efficient, making the merging of sequences efficient, and processing data in a defined order.[1] Sorting is also often useful for canonicalising data and for producing human-readable output.[3]

In PostgreSQL, rows returned without a sort step come in an order that depends on the scan and join plan types and the order on disk, and that order must not be relied on.[4] In SQL, an ORDER BY placed in a subquery or a view definition, where a database system allows one, has no effect on the order of the final result.[2] In DuckDB, WHERE, LIMIT and SELECT guarantee that the original row order is preserved, while JOIN, GROUP BY and UNION do not guarantee it.[5] Sort as the last step of the work, after the joins and aggregations, rather than counting on an earlier sort to survive them. (Meridian's view)

In DuckDB, ORDER BY may not use a stable algorithm, so it does not guarantee that the original row order is preserved.[5] In DuckDB, row_number() OVER () turns the original row order into an explicit column that operations which do not preserve row order can refer to.[5] When the order rows arrived in matters, number them before sorting and add that number as the last sort key, so that rows tied on every other key come out the same way on every run. (Meridian's view)

The SQL standard's core functionality does not explicitly define a default sort order for nulls, and its SQL:2003 extension T611 lets nulls be sorted first or last with NULLS FIRST or NULLS LAST.[2] In PostgreSQL, nulls sort as if larger than any non-null value by default, so NULLS FIRST is the default for DESC order and NULLS LAST otherwise.[4] Write NULLS FIRST or NULLS LAST into any sort whose result will be compared with one from another database system. (Meridian's view)

In DuckDB, text is sorted by its binary UTF-8 values by default, which works well for ASCII text but can give an incorrect order for other languages.[6] In DuckDB, collations such as NOCASE and NOACCENT change how text is compared, and a collation can be set on an expression, on a table column or as the database default.[7] Choose the collation deliberately when people will read the sorted list, and keep the binary order when the sort only needs to be repeatable. (Meridian's view)

The method

Formally, the output of any sorting algorithm must be in monotonic order and must be a permutation of the input, a reordering that retains all of the original elements.[3] When records are sorted on one or more of their components, each such component is called a sort key.[1] A new sort key can be created from two or more sort keys by lexicographical order, the first being the primary sort key and the second the secondary sort key.[1] In PostgreSQL, when ORDER BY names more than one expression, the later values are used to sort rows that are equal according to the earlier values.[4] In PostgreSQL, the ordering options apply to each sort column independently, so ORDER BY x, y DESC means ORDER BY x ASC, y DESC.[4] In PostgreSQL, ascending order puts smaller values first, as defined by the < operator, and descending order is determined with the > operator.[4]

A sorting algorithm is stable if, whenever two records have the same key and one appears before the other in the original list, it also appears before the other in the sorted list.[3] An unstable sorting algorithm can be made stable by deciding comparisons between otherwise equal keys on the order of the entries in the original input, which may require additional time and space.[3] Stability is important for preserving order over several sorts of the same data, since with an unstable sort a later sort could scramble the order an earlier one produced.[3] Among the authors of early sorting algorithms around 1951 was Betty Holberton, who worked on ENIAC and UNIVAC.[3]

In DuckDB, ORDER BY is an output modifier, applied logically near the very end of the query, just before LIMIT or OFFSET if present.[6] External sorting is required when the data being sorted does not fit into a computer's main memory and must instead reside in slower external memory, usually a disk drive.[8] In Apache Hop, the Sort Rows transform starts to page data to disk when it finds less free memory than a set threshold, keeping its temporary files in a sort directory.[9] In Apache Hop, the Sort Rows transform is not supported on the Beam engines, because a Beam pipeline spreads rows over many workers and the transform could only order the rows a single worker holds.[9]

The opposite of sorting, rearranging a sequence of items in a random or meaningless order, is called shuffling.[1] The Fisher–Yates shuffle produces an unbiased permutation of a finite sequence, in which every permutation is equally likely.[10] The Fisher–Yates shuffle was published in 1938 by Ronald Fisher and Frank Yates in their book Statistical tables for biological, agricultural and medical research.[10]

Compared with the alternatives

In Power Query M, Table.ReverseRows returns a table with the rows of the input table in reverse order.[11] Reverse the rows only when their current order is itself meaningful; otherwise sort descending on a named column. (Meridian's view)

In OpenRefine, a sort is temporary and the rows keep their original numbering until Reorder rows permanently is chosen, which changes the row numbers.[12] Treat a sort shown on screen and a sort written into the stored data as two different steps, and record which one a result depends on. (Meridian's view)

Used in

One Protocol uses this operation: