Skip to content

Instantly share code, notes, and snippets.

@kuno
Created January 20, 2011 07:34
Show Gist options
  • Save kuno/787534 to your computer and use it in GitHub Desktop.
Save kuno/787534 to your computer and use it in GitHub Desktop.
new compareVersion method
var notSmaller, arrayA = versionA.split('-'), arrayB = versionB.split('-'),
minorA, minorB, mainA = arrayA[0].split('.'), mainB = arrayB[0].split('.'),
len = (mainA.length <= mainB.length) ? mainA.length : mainB.length;
for (var i = 0; i < len; ++i) {
if (parseInt(mainA[i], 10) < parseInt(mainB[i], 10)) {
notSmaller = -1;
break;
} else if (parseInt(mainA[i], 10) > parseInt(mainB[i], 10)) {
notSmaller = 1;
break;
} else if (i === len - 1) {
if (mainA.length > mainB.length) {
notSmaller = 1;
} else if (mainA.length === mainB.length) {
if (arrayA.length === arrayB.length === 1) {
notSmaller = 0;
} else if (arrayA.length > arrayB.length) {
notSmaller = 1;
} else if (arrayA.length < arrayA.length) {
notSmaller = -1;
} else {
minorA = arrayA.reverse()[0].replace(/[a-zA-Z]/,'');
minorB = arrayB.reverse()[0].replace(/[a-zA-Z]/,'');
if (parseInt(minorA, 10) === parseInt(minorB, 10)) {
notSmaller = 0;
} else if (parseInt(minorA, 10) > parseInt(minorB, 10)) {
notSmaller = 1;
} else if (parseInt(minorA, 10) < parseInt(minorB, 10)) {
notSmaller = -1;
}
}
} else {
notSmaller = -1;
}
}
}
return notSmaller;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment