Suppose your release job picks the last line of a sorted list. Its list had only two lines: 1.10.0 and 1.10.0-rc.1. On GNU sort, the last line was the release candidate. For a file listing, that order is harmless. For a job that promises Semantic Versioning precedence, it picks the wrong artifact.

These examples use synthetic names in a local lab. No registry or deployment is involved.

Reproduce both sorting mistakes

With whole version labels, plain sorting compares their text. It sees the 1 in 10 before the 9, even though minor version 10 is newer. GNU version sort gets this simple case right.

Actual shell output comparing ordinary sort and GNU version sort

printf '%s\n' v1.9.0 v1.10.0 v1.2.0 | LC_ALL=C sort printf '%s\n' v1.9.0 v1.10.0 v1.2.0 | LC_ALL=C sort -V 

The default sort and the version sort disagree at the first differing minor component. The leading v is just part of these file labels; it isn't needed for the version-sort comparison.

Sequence strip showing the minor version as the first different component

Now hold the numeric part constant and change only the suffix. On GNU sort, the final release sorts before the release candidate, so tail -n 1 picks the candidate.

Actual GNU sort output putting the release candidate last

printf '%s\n' 1.10.0 1.10.0-rc.1 | LC_ALL=C sort -V 

SemVer's rule for that tie goes the other way: the final release has higher precedence than its prerelease. You can use sort -V to browse numbered artifact names without making it the rule that selects a release.

Side-by-side comparison of last entries under the two sorting rules

Decide whether prereleases are eligible

For a stable channel, discard prereleases before picking the highest version. Otherwise, 2.0.0-rc.1 outranks 1.10.0: the higher major number is compared before either suffix.

Branch diagram showing the stable and preview policies

Here's a small stable-channel selector using the Node semver library. Run npm install semver in a scratch project, then save the example as pick.cjs. It expects version labels rather than arbitrary filenames.

const semver = require('semver'); function latestStable(labels) { const candidates = labels.map(label => { const v = label.startsWith('v') ? label.slice(1) : label; if (!semver.valid(v)) { throw new Error(`Invalid version: ${label}`); } return { label, v }; }); const stable = candidates .filter(({ v }) => semver.prerelease(v) === null) .sort((a, b) => semver.rcompare(a.v, b.v)); if (!stable.length) { throw new Error('No stable release'); } if (stable[1] && semver.eq(stable[0].v, stable[1].v)) { throw new Error('Tie: choose artifacts by another rule'); } return stable[0].label; } module.exports = latestStable; 

Parsing happens before filtering: a typo like 1.10 is surfaced rather than quietly thrown out. The selector returns the original label, so a caller can look up the same tag it passed in.

Process diagram showing validation followed by filtering and comparison

Build metadata is useful for tracing a build, but SemVer gives 1.10.0+a and 1.10.0+b the same precedence. Resolve that tie with a rule from your project, such as the artifact digest recorded by the release job, rather than choosing whichever label arrived first.

Two build labels converging onto the same precedence level

Put the counterexample in a test

Test which labels are eligible, not just which one sorts last. One case checks that the stable tag wins; another passes only prereleases and expects a clear failure. An explicit tie test keeps build metadata from silently becoming a sort key.

Actual Node test output for four small release-selection fixtures

const assert = require('node:assert/strict'); const latestStable = require('./pick.cjs'); const tags = ['v1.9.0', 'v1.10.0', 'v2.0.0-rc.1']; assert.equal(latestStable(tags), 'v1.10.0'); assert.throws(() => latestStable(['1.10.0-rc.1']), /No stable release/); assert.throws(() => latestStable(['v1.10']), /Invalid version/); assert.throws(() => latestStable(['1.10.0+a', '1.10.0+b']), /Tie/); 

Use the simpler command when the output is for a person browsing files. When that last line starts a release step, make eligibility and precedence part of the test.

Decision checklist separating file browsing from release selection

References: GNU version sort, Semantic Versioning 2.0.0 precedence, npm node-semver.

AI disclosure: AI wrote this post and ran the local examples. All visuals use synthetic inputs.