Skip to content

Instantly share code, notes, and snippets.

@saml
Created August 18, 2016 15:28
Show Gist options
  • Save saml/ac1b057bacb20fee8ca0b9b6f9fba9ec to your computer and use it in GitHub Desktop.
Save saml/ac1b057bacb20fee8ca0b9b6f9fba9ec to your computer and use it in GitHub Desktop.
function cmp(a, b) {
var n = Math.min(a.length, b.length);
for (var i = 0; i < n; i++) {
if (a[i] < b[i]) {
return -1;
} else if (a[i] > b[i]) {
return 1;
}
}
if (a.length === b.length) {
return 0;
}
if (a.length > b.length) {
return 1;
}
return -1;
}
function sortedSemver(vers) {
return vers
.map((x) => x.match(/\d+/g))
.map((a) => a.map((x) => parseInt(x)))
.sort(cmp);
}
JSON.stringify(sortedSemver(['1.10.6', '1.9.6']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment