What you will build
A generated report should have the same order on a developer machine and in a build job. Locale-dependent collation and text comparison of numbers can otherwise create noisy differences. Fix the locale for each sort invocation and give each key an explicit field boundary.
The fixture groups names alphabetically, then orders integer counts within each name. It includes 2 and 10, making a mistaken lexical number sort visible. Compare actual bytes with an independently written expected file and check that the original unsorted input is rejected.
Prerequisites
Bash and GNU coreutils, including sort, mktemp and cmp. Run in a writable scratch directory. The input contract is exactly two simple comma-delimited fields, with ASCII names and integer values; it is not general CSV. Save as sort.sh.
#!/usr/bin/env bash
set -euo pipefail
work=$(mktemp -d ./sort-fixture.XXXXXX)
trap 'rm -rf -- "$work"' EXIT
printf '%s\n' 'beta,2' 'alpha,10' 'alpha,2' 'beta,1' > "$work/input"
LC_ALL=C sort -t, -k1,1 -k2,2n "$work/input" > "$work/actual"
printf '%s\n' 'alpha,2' 'alpha,10' 'beta,1' 'beta,2' > "$work/expected"
cmp "$work/expected" "$work/actual"
LC_ALL=C sort -c -t, -k1,1 -k2,2n "$work/actual"
if LC_ALL=C sort -c -t, -k1,1 -k2,2n "$work/input" 2>/dev/null; then
echo 'unsorted fixture was accepted' >&2
exit 1
fi
cat "$work/actual"
printf '%s\n' 'order and negative check passed'
Run and expected output
bash sort.shalpha,2
alpha,10
beta,1
beta,2
order and negative check passedHow it works
The comma delimiter selects fields. The first key, -k1,1, starts and ends at field one. The second, -k2,2n, selects only field two and compares it numerically. Explicit end positions matter when a report gains columns: an open-ended key can incorporate the rest of a record unexpectedly.
Every sort command is prefixed with LC_ALL=C, giving these ASCII names predictable byte-oriented collation instead of relying on the user's locale. The GNU sort documentation explains locale behavior, key syntax, numeric comparison and check mode.
The byte comparison verifies the intended result, while sort -c confirms the ordering relation. These answer different questions: a file might be sorted but missing a row. The negative fixture proves check mode detects disorder instead of merely succeeding for every input.
Edge cases and scope
This is a restricted delimited format. A quoted comma or newline inside a field requires a CSV parser; -t does not understand quoting. Validate the producer contract before applying this to spreadsheet exports. Headers also require separate handling because sort treats them as ordinary lines.
When selected keys compare equal, GNU sort normally uses a final whole-line comparison. If your contract requires preserving input order among equal keys, investigate stable mode and add a fixture exercising it. This sample does not need that policy because every name/count pair is distinct.
Large files may cause sort to use temporary storage. This small fixture validates ordering semantics, not disk capacity or throughput. Keep output paths distinct from inputs so shell redirection cannot truncate the source before sort reads it.
Troubleshooting
If 10 appears before 2, check the numeric flag on the second key. If machines disagree, inspect every invocation for the locale setting instead of assuming login environments match. If check mode fails, remove stderr suppression to see the first disorder location. The trap removes only fixture files created by this script.
Primary references
Continue with Linux tutorials and all Java Samples categories.