Skip to content

Instantly share code, notes, and snippets.

@ngocongcan
Last active April 10, 2017 08:26
Show Gist options
  • Save ngocongcan/e9d35e611d9ea83d3331fa6f3fa02eec to your computer and use it in GitHub Desktop.
Save ngocongcan/e9d35e611d9ea83d3331fa6f3fa02eec to your computer and use it in GitHub Desktop.
function cmpVersions (a, b) {
var i, diff;
var regExStrip0 = /(\.0+)+$/;
var segmentsA = a.replace(regExStrip0, '').split('.');
var segmentsB = b.replace(regExStrip0, '').split('.');
var l = Math.min(segmentsA.length, segmentsB.length);
for (i = 0; i < l; i++) {
diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10);
if (diff) {
return diff;
}
}
return segmentsA.length - segmentsB.length;
}
// TEST
console.log(
['2.5.10.4159',
'1.0.0',
'0.5',
'0.4.1',
'1',
'1.1',
'0.0.0',
'2.5.0',
'2',
'0.0',
'2.5.10',
'10.5',
'1.25.4',
'1.2.15'].sort(cmpVersions));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment