Skip to the code
cf.
+25 XP

#218 Order Package Versions

medium

A dependency dashboard lists published versions of a package, like "1.10.0" and "1.2". Versions must be ordered by their numeric parts, not as text: 1.2.0 comes before 1.10.0.

Compare two versions part by part, as numbers, from left to right. A missing part counts as 0, so "1.2" and "1.2.0" are equal. Versions that are equal must keep their original order.

Write sortVersions(versions) that returns a new array with the versions in ascending order.

Example 1

Input: versions = ["1.10.0","1.2.0","1.9"]
Output: ["1.2.0","1.9","1.10.0"]

Second parts compare as numbers: 2 < 9 < 10.

Example 2

Input: versions = ["2.0","1.2.0","1.2"]
Output: ["1.2.0","1.2","2.0"]

1.2.0 and 1.2 are equal, so they keep their original order.

Constraints

  • 0 <= versions.length <= 10000
  • Each version is 1 to 4 whole numbers from 0 to 10^6 joined by ..
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).