Sorting Model Versions as Numbers, Not Text

MLOpsEasy+10 XP+10

Sorting Model Versions as Numbers, Not Text

Model artefacts get version strings like "1.4.2" — major, minor, patch. Sorting them is where people get caught, because sorting them as text is wrong: alphabetically, "1.10.0" comes before "1.9.0", since the character 1 sorts before 9. A deploy script that trusts that will happily roll out an older model as the newest one.

The fix is to compare the parts as numbers.

Task: write sort_versions(versions) returning the versions ordered newest first.

  • Every version is a string of three numbers separated by dots: "major.minor.patch".
  • Split on ., convert each part to an integer, and compare the triples. Major decides first; minor only breaks a tie on major; patch only breaks a tie on both.
  • Return the original strings, not the parsed numbers — just reordered.
  • Versions are distinct, so there are no ties to break.
  • A single version comes back as a one-item list.

Python compares lists element by element, so once you've turned "1.10.0" into [1, 10, 0] the comparison you want is the one you get for free. That's the whole trick: convert to a shape whose default ordering is already correct, rather than writing the comparison yourself.

Examples

versions = ['1.2.3', '1.10.0', '1.9.9']
returns ['1.10.0', '1.9.9', '1.2.3']
versions = ['0.9.0', '1.0.0', '0.10.0']
returns ['1.0.0', '0.10.0', '0.9.0']

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run