Deviopath › TypeScript

Sort Semantic Versions

Hard · Algorithms

Versions look like MAJOR.MINOR.PATCH with an optional pre-release part after a -, e.g. "1.0.0-beta.2". Write sortVersions(versions) that returns a new array sorted from lowest to highest precedence:

1. Compare MAJOR, MINOR and PATCH numerically.

2. If they are equal, a version WITHOUT pre-release is higher than one with it.

3. Otherwise compare the pre-release identifiers (split on .) one by one: two numeric identifiers compare numerically; a numeric identifier is lower than a non-numeric one; two non-numeric identifiers compare by plain string comparison (<). If all compared identifiers are equal, the version with fewer identifiers is lower.

Equal versions keep their original order.

Next: Typed Event Emitter