Skip to content

sortVersions

Sorts version strings by version, not by text (1.10.0 after 1.9.0), in the given scheme: the same as versions.sort((a, b) => compare(a, b, scheme)), without mutating the input. Versions that compare as equal keep their order. Strings are returned as they were written (a v prefix stays).

Every version is compared with compare, so in the schemes that throw on invalid input ('gentoo', 'debian', …) a bad version throws a SyntaxError: filter with isValid first.

Available since v3.1.9

import { sortVersions } from '@helpers4/version';
// or, from the all-in-one package (same code, one install):
import { sortVersions } from 'helpers4/version';
sortVersions<T extends string>(versions: readonly T[], options: SortVersionsOptions): T[]
ParameterTypeDescription
versionsreadonly T[]The versions to sort
optionsSortVersionsOptionsOrder and scheme — see SortVersionsOptions

T[] — A new sorted array

1.10.0 comes after 1.9.0, which a plain sort() gets wrong.

sortVersions(['1.10.0', '1.2.0', '1.9.0'])
// => ['1.2.0', '1.9.0', '1.10.0']

Newest first; the v prefix of git tags is kept.

sortVersions(['v1.0.0', 'v2.0.0-rc.1', 'v1.5.0'], { order: 'desc' })[0]
// => 'v2.0.0-rc.1'

Options for sortVersions.

interface SortVersionsOptions {
  order?: "desc" | "asc";
  scheme?: VersionScheme;
}

View source on GitHub