Skip to content

Instantly share code, notes, and snippets.

@zfkun
Created February 11, 2015 16:31
Show Gist options
  • Save zfkun/074e0488f0119a97a8f1 to your computer and use it in GitHub Desktop.
Save zfkun/074e0488f0119a97a8f1 to your computer and use it in GitHub Desktop.
version compare toolkit
/**
* 版本对比
*
* @param {string} a 对比版本号
* @param {string} b 参考版本号
* @return {number} `1`(`a > b`) || `-1`(`a < b`) || `0`(`a = b`)
*/
function versionCompare(a, b) {
a = a.split('.');
b = b.split('.');
for (var i = 0, n = Math.max(a.length, b.length); i < n; i++) {
a[i] = parseInt(a[i], 10) || 0;
b[i] = parseInt(b[i], 10) || 0;
if (a[i] === b[i]) {
continue;
}
return a[i] > b[i] ? 1 : -1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment