SemVer compare and sort

Compare two semantic versions, including prerelease precedence, or sort a list of versions.

Input

The first version.
The second version.
A list of versions to sort from lowest to highest.
Sort from highest to lowest.

Result

Fill in the input and press Run.

Your input is processed in memory and then discarded. It is not stored or logged. Even so, don't paste live passwords or keys into any online tool.

Use it from code or an agent

curl -s https://setget.net/v1/tools/semver-compare \
  -H 'Content-Type: application/json' \
  -d '{"input":{"a":"1.0.0-alpha","b":"1.0.0"}}'
import json, urllib.request

body = {'input': {'a': '1.0.0-alpha', 'b': '1.0.0'}}
request = urllib.request.Request("https://setget.net/v1/tools/semver-compare", json.dumps(body).encode(),
                                 {"Content-Type": "application/json"})
print(json.load(urllib.request.urlopen(request))["result"])
const response = await fetch("https://setget.net/v1/tools/semver-compare", {
  method: "POST",
  headers: {"Content-Type": "application/json"},
  body: JSON.stringify({"input":{"a":"1.0.0-alpha","b":"1.0.0"}}),
});
console.log((await response.json()).result);
Tool: run_tool
Arguments:
{
  "slug": "semver-compare",
  "input": {
    "a": "1.0.0-alpha",
    "b": "1.0.0"
  }
}
{
  "input": {
    "a": "1.0.0-alpha",
    "b": "1.0.0"
  }
}

Endpoint: POST /v1/tools/semver-compare. Over MCP: run_tool with slug: "semver-compare". Machine-readable: input schema, Markdown. Connect an agent.

Inputs

NameTypeRequiredWhat it does
a string no The first version.
b string no The second version.
versions array no A list of versions to sort from lowest to highest.
descending boolean no Sort from highest to lowest. Default: false.

Examples

Input
{
  "a": "1.0.0-alpha",
  "b": "1.0.0"
}
Result
{
  "comparison": {
    "a": {
      "version": "1.0.0-alpha",
      "major": 1,
      "minor": 0,
      "patch": 0,
      "prerelease": [
        "alpha"
      ],
      "build": []
    },
    "b": {
      "version": "1.0.0",
      "major": 1,
      "minor": 0,
      "patch": 0,
      "prerelease": [],
      "build": []
    },
    "result": -1,
    "relation": "a < b",
    "difference": "prerelease"
  },
  "sorted": null
}

The precedence example from the SemVer specification

Input
{
  "versions": [
    "1.0.0",
    "1.0.0-rc.1",
    "1.0.0-beta.11",
    "1.0.0-beta.2",
    "1.0.0-beta",
    "1.0.0-alpha.beta",
    "1.0.0-alpha.1",
    "1.0.0-alpha"
  ]
}
Result
{
  "comparison": null,
  "sorted": [
    "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"
  ]
}

Questions

Why is 1.0.0-beta.11 higher than 1.0.0-beta.2?
Numeric prerelease parts are compared as numbers, not as text.
Does build metadata matter?
No. Anything after "+" is ignored for precedence, so 1.0.0+a and 1.0.0+b are equal. The difference is then reported as "build".
Is a leading "v" allowed?
Yes, v1.2.3 is read as 1.2.3. Everything else must follow SemVer 2.0.0 strictly.