Release order
EasyOnline Assessment
Sort software version strings from oldest to newest.
A version string has an optional leading v or V, then one to five integers separated by dots, then optionally a pre-release tag: -alpha, -beta or -rc. Examples: "1.4", "v2.0.1", "3.1-rc".
Versions are compared as follows:
- The
v/Vprefix is ignored. - The dotted integers are compared left to right as numbers (
1.10is newer than1.9). If one version has fewer integers, the missing ones count as 0, so1.2and1.2.0are the same number. - If the numbers are equal, a pre-release is older than the plain release, and the pre-release tags are ordered
alpha<beta<rc< no tag. - Versions that compare as equal keep their original relative order from the input (they are not deduplicated and not rewritten).
Input: versions, a list of version strings.
Output: a list with the same strings, unchanged, ordered from oldest to newest. If versions is empty, return [].
Example 1
- Input:
- [["1.10","1.9","1.2.1","1.2"]]
- Output:
- ["1.2","1.2.1","1.9","1.10"]
- Explanation
- Components are compared as numbers: 1.2 < 1.2.1 < 1.9 < 1.10 (10 is larger than 9, unlike in dictionary order). Each missing part would count as 0.
Example 2
- Input:
- [["2.0-rc","2.0","2.0-beta","2.0-alpha","1.9.9"]]
- Output:
- ["1.9.9","2.0-alpha","2.0-beta","2.0-rc","2.0"]
- Explanation
- 1.9.9 is below every 2.0 build. For 2.0, the order is alpha, beta, rc, then the plain release.
Example 3
- Input:
- [["v1.2.0","1.2","V1.1","1.2.0-beta","1.2"]]
- Output:
- ["V1.1","1.2.0-beta","v1.2.0","1.2","1.2"]
- Explanation
- "V1.1" is smallest (the prefix is ignored). 1.2.0-beta comes before any 1.2 release. "v1.2.0", "1.2" and "1.2" are the same version (1.2 equals 1.2.0), so they keep their input order.
Constraints
0 <= versions.length <= 2,000- Each version is an optional
vorV, then 1 to 5 dot-separated integers (0 to 999, no leading zeros except0itself), then optionally-alpha,-betaor-rc