Skip to content
Latchkey

sort -n: Numeric Sorting in CI Pipelines

sort -n sorts lines as numbers, so 9 comes before 10 instead of after it.

Plain sort compares strings, so "10" sorts before "9". When the field is a number, -n gives the order you actually want.

What it does

sort -n interprets the leading numeric portion of each line (or sort key) as a number and orders by value. Without -n, sort compares character by character, so "10" precedes "2". -g uses a general-numeric comparison that also understands scientific notation like 1e3.

Common usage

Terminal
sort -n sizes.txt
du -b * | sort -n            # smallest files first
sort -nr counts.txt          # largest numbers first (-r reverses)

Options

FlagWhat it does
-nCompare by leading numeric value
-gGeneral numeric: also handles 1.5e3, inf
-rReverse the result order
-k <pos>Sort by a specific field instead of the whole line
-bIgnore leading blanks when finding the key

In CI

Pipe metrics or sizes through sort -n before picking the top or bottom value with head or tail. -g is much slower than -n; only reach for it when the data really has floats or exponents.

Common errors in CI

In some locales the thousands separator and decimal comma confuse -n, so 1,5 may not sort as expected; set LC_ALL=C for deterministic, value-based ordering. -n only reads the leading number, so "v9" and "v10" both parse as 0 and stay in input order; strip the prefix or use -V version sort instead.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →