Skip to content
Latchkey

sort -V: Version-Aware Sorting

sort -V sorts version numbers naturally, so v1.2.10 comes after v1.2.9.

Version strings break ordinary sort: "1.10" lands before "1.9". -V (version sort) understands the dotted, numbered structure.

What it does

sort -V compares strings using a "natural" rule that treats embedded numbers as numbers, so 1.2.10 sorts after 1.2.9 and v10 after v9. It is purpose-built for version tags, release names, and filenames with numeric runs.

Common usage

Terminal
git tag | sort -V                 # tags in release order
git tag | sort -V | tail -1       # latest tag
ls backups/ | sort -V             # natural filename order

Options

FlagWhat it does
-VNatural version sort of embedded numbers
-rReverse: newest version first with -Vr
-uUnique after version sort
-k <pos>Version-sort a specific field

In CI

git tag | sort -V | tail -1 is the standard way to find the highest semver tag, since the tags arrive unordered. Use sort -Vr | head -1 if you prefer head.

Common errors in CI

-V is GNU-only; BSD/macOS sort rejects it with "illegal option -- V". On macOS runners install coreutils and use gsort -V, or fall back to sort -t. -k. Pre-release suffixes like 1.0.0-rc1 do not follow semver precedence under -V; it sorts them lexically after 1.0.0, which differs from true semver rules.

Related guides

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