sort: Usage, Options & Common CI Errors
sort arranges input lines in order - alphabetical by default, numeric or by key on request.
sort is essential for deterministic output (stable diffs, deduped lists). The hidden gotcha is locale: the same command can order differently across runner images unless you pin LC_ALL.
What it does
sort reads lines, orders them by the collating rules of the current locale (or numerically/by key when told), and writes them out. With -u it also removes duplicate lines.
Common usage
sort file.txt
sort -n nums.txt # numeric, not lexicographic
sort -u list.txt # sort + remove duplicates
sort -k2 -t, data.csv # sort by 2nd comma-field
sort -rn -k3 sizes.txt # reverse numeric on field 3
LC_ALL=C sort file.txt # stable, byte-order sortingOptions
| Flag | What it does |
|---|---|
| -n / -g | Numeric / general-numeric sort |
| -u | Output unique lines only |
| -k <field> | Sort by a key field |
| -t <char> | Field separator |
| -r | Reverse order |
| -h | Human-numeric (2K, 1G) |
Common errors in CI
Plain sort is locale-sensitive: a runner with a UTF-8 locale orders case and accents differently than LC_ALL=C, so checked-in "sorted" files fail to compare across machines - pin LC_ALL=C for reproducible byte ordering. Sorting numbers without -n gives 1,10,2,3 (lexicographic). "sort: cannot read: file: No such file or directory" exits 2. Note sort -u dedups identical whole lines; for adjacent-only dedup or counts use uniq.