Skip to content

Instantly share code, notes, and snippets.

@joeylin
Created April 6, 2016 03:20
Show Gist options
  • Save joeylin/1a731791827477a29c21abdece504433 to your computer and use it in GitHub Desktop.
Save joeylin/1a731791827477a29c21abdece504433 to your computer and use it in GitHub Desktop.
function versionCompare(a, b) {
var i;
var len;
if (typeof a + typeof b !== 'stringstring') {
return false;
}
a = a.split('.');
b = b.split('.');
len = Math.max(a.length, b.length);
for (i = 0; i < len; i++) {
if ((a[i] && !b[i] && parseInt(a[i]) > 0) || (parseInt(a[i]) > parseInt(b[i]))) {
return 1;
} else if ((b[i] && !a[i] && parseInt(b[i]) > 0) || (parseInt(a[i]) < parseInt(b[i]))) {
return -1;
}
}
return 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment