Skip to content
Latchkey

sort Command Reference for CI Scripts

sort arranges input lines in order, alphabetical by default or numeric 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.

Common flags/usage

  • -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)

Example

shell
LC_ALL=C sort -u allowed-hosts.txt > sorted.txt
du -h --max-depth=1 . | sort -rh | head -n 10
sort -t, -k2 -n data.csv

In CI

Plain sort is locale-sensitive, so a runner with a UTF-8 locale orders case and accents differently than LC_ALL=C, and 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 (lexicographic). sort -u dedups whole lines.

Key takeaways

  • Pin LC_ALL=C for byte-order sorting that is stable across runner images.
  • Use -n for numbers; lexicographic order puts 10 before 2.
  • sort -u dedups whole lines in one pass without a separate uniq.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →