# SemVer compare and sort

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

- Endpoint: `POST https://setget.net/v1/tools/semver-compare` with JSON `{"input": {...}}`
- MCP: `run_tool` with `slug` = `semver-compare`
- Page: https://setget.net/tools/semver-compare

## Inputs

- `a` (optional, string): The first version.
- `b` (optional, string): The second version.
- `versions` (optional, array): A list of versions to sort from lowest to highest.
- `descending` (optional, boolean, default `false`): Sort from highest to lowest.

## Example 1

```json
{"input":{"a":"1.0.0-alpha","b":"1.0.0"}}
```

Result:

```json
{"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}
```

## Example 2: The precedence example from the SemVer specification

```json
{"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:

```json
{"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"]}
```

**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.
