Sort Semantic Versions
Medium · npm
npm versions follow semver: MAJOR.MINOR.PATCH, optionally followed by -prerelease and +build. Write sortVersions(list) that returns a new array sorted from lowest to highest, keeping each string exactly as given.
Rules (from semver.org):
- An optional leading v is ignored. +build is ignored for ordering.
- Compare MAJOR, MINOR, PATCH as numbers (2.10.0 > 2.9.0).
- A version WITH a prerelease is lower than the same version without one: 1.0.0-rc.1 < 1.0.0.
- Prereleases are compared identifier by identifier (split on .): numeric identifiers compare as numbers, numeric is lower than text, text compares in ASCII order (so "Beta" < "alpha"). If all shared identifiers are equal, the shorter prerelease is lower.
- Versions with equal precedence keep their original order.
Example order: 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0.
Next: Caret and Tilde Ranges