Skip to content

Instantly share code, notes, and snippets.

@claudiohilario
Last active May 17, 2020 22:30
Show Gist options
  • Save claudiohilario/fb45ce7bace487b5fd52356cf88ed6a2 to your computer and use it in GitHub Desktop.
Save claudiohilario/fb45ce7bace487b5fd52356cf88ed6a2 to your computer and use it in GitHub Desktop.
Compare versions x.x.x
/**
* This function allows you to compare two versions in x.x.x format.
*
* Conditions:
* v1 == v2 = 0
* v1 > v2 = 1
* v1 < v2 = -1
*
* @example
* compareVersions('0.0.0', '0.0.1');
*
* @param {string} v1 - Version One.
* @param {string} v2 - Version Two.
*
* @returns {number} - Returns a number that represents the comparison.
* E.g.: -1;
*/
function compareVersions(v1, v2) {
if( v1 === v2) {
return 0;
}
const arrV1 = v1.split('.').map(number => parseInt(number));
const arrV2 = v2.split('.').map(number => parseInt(number));
for(let j = 0; j < 3; j++) {
if(arrV1[j] == arrV2[j]) { continue; }
return arrV1[j] > arrV2[j] ? 1 : -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment