Given two version numbers such as 0.20.7 and 0.20.8 you want to know which comes first. The version numbers are a few integer numbers that are concatenated by dots and sometimes a small number (usually the last one) skipped is usually the patch version. For example, 0.20.7.1 is usually the patch version of version number 0.20.7 and therefore “0.20.7.1” is greater than “0.20.7”
But the version numbers could have leading zeros in each number, for example, ‘0.20.07’ is the same as ‘0.20.7’ therefore you should not compare the strings directly as it will return incorrect result.
Javascript: compare Version v1
The following Javascript function compares two version number strings – and return false in case input is not a string. Otherwise, it will split each version number by delimiter dot and compare each sub-group one by one – numerically (by converting the string to decimal integers via parseInt).
If the number of the input version groups is different, the result is treated as adding more “.0” to the smaller version for example, comparing “0.1” and “0.1.2” is same as comparing “0.1.0” to “0.1.2”
function compareVersion(v1, v2) {
if (typeof v1 !== 'string') return false;
if (typeof v2 !== 'string') return false;
v1 = v1.split('.');
v2 = v2.split('.');
const k = Math.min(v1.length, v2.length);
for (let i = 0; i < k; ++ i) {
v1[i] = parseInt(v1[i], 10);
v2[i] = parseInt(v2[i], 10);
if (v1[i] > v2[i]) return 1;
if (v1[i] < v2[i]) return -1;
}
return v1.length == v2.length ? 0: (v1.length < v2.length ? -1 : 1);
}
Here are some test cases that demonstrate the usages
console.log(compareVersion("0.20.7", "0.20.8")); // -1
console.log(compareVersion("0.20.9", "0.20.8")); // 1
console.log(compareVersion("0.20.08", "0.20.8")); // 0
console.log(compareVersion("0.20.08", "0.20.8.1")); // -1
console.log(compareVersion("0.20.8.1", "0.20.8")); // 1
console.log(compareVersion("0.020", "0.20")); // 0
console.log(compareVersion(0.1, 0.2)); // false
console.log(compareVersion("0", "0")); // 0
console.log(compareVersion("0.1", true)); // false
Javascript: compare Version v2 – another version
Here is another JavaScript function to compare version strings:
function compareVersion(version1, version2) {
const v1 = version1.split('.').map(Number);
const v2 = version2.split('.').map(Number);
const len = Math.max(v1.length, v2.length);
for (let i = 0; i < len; i++) {
const num1 = v1[i] || 0; // Treat missing parts as 0
const num2 = v2[i] || 0;
if (num1 > num2) {
return 1;
} else if (num1 < num2) {
return -1;
}
}
return 0; // Versions are equal
}
// Test cases
console.log(compareVersion("0.23.1", "0.23.0")); // Output: 1
console.log(compareVersion("0.23.0", "0.23.0")); // Output: 0
console.log(compareVersion("0.22.99", "0.23.0")); // Output: -1
Explanation:
- It splits each version string by the dot (.) and converts each segment to a number.
- It compares corresponding parts of the two versions.
- If one version has fewer parts than the other, the missing parts are treated as 0 (e.g., “1.0” is equivalent to “1.0.0”).
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: Finding Two Numbers of Given Sum in Binary Search Tree
Next Post: Pros and Cons of Having SEO for White Label Services