The Javascript Function to Compare Version Number Strings


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.

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”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
}
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

1
2
3
4
5
6
7
8
9
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
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
JS The Javascript Function to Compare Version Number Strings javascript

NodeJs / Javascript

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
382 words
Last Post: Finding Two Numbers of Given Sum in Binary Search Tree
Next Post: Pros and Cons of Having SEO for White Label Services

The Permanent URL is: The Javascript Function to Compare Version Number Strings

One Response

  1. Balaji

Leave a Reply