You are given n version strings in a simplified semantic-versioning format: MAJOR.MINOR.PATCH, each of MAJOR/MINOR/PATCH a non-negative integer with no extra leading zero characters beyond the value 0 itself, optionally followed by a pre-release suffix -ID1.ID2. ... .IDk (a - followed by one or more dot-separated identifiers; an identifier consists of ASCII letters, digits, and hyphens, and is never empty).
Sort the versions in ascending precedence order using these rules:
1.0.0-1 and 1.0.0-01), preserve their original relative order from the input (a stable sort).Line 1: an integer n.
Lines 2..n+1: one version string per line.
The n version strings, one per line, in ascending precedence order.
[A-Za-z0-9-]Example 1
Input
3 1.0.0 1.0.0-alpha 1.2.0
Expected
1.0.0-alpha 1.0.0 1.2.0
Explanation
`1.0.0-alpha` has the same core as `1.0.0` but is a pre-release, so it has lower precedence than `1.0.0`; `1.2.0` has a higher minor version than both, so it comes last.
Example 2
Input
2 2.0.0-1 2.0.0-alpha
Expected
2.0.0-1 2.0.0-alpha
Explanation
Both share the same core and have one pre-release identifier each; `1` is purely numeric while `alpha` is not, so `1` has lower precedence and sorts first.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →