How to compare software version number using js? (only number)

How to compare software version number using js? (only number)

compare version numbers c#
npm version compare
compare version numbers python
semver
sort version numbers java
sql compare version strings
scala version compare
react version comparison

Here is the software version number:

"1.0", "1.0.1", "2.0", "2.0.0.1", "2.0.1"

How can I compare this?? Assume the correct order is:

"1.0", "1.0.1", "2.0", "2.0.0.1", "2.0.1"

The idea is simple...: Read the first digit, than, the second, after that the third.... But I can't convert the version number to float number.... You also can see the version number like this:

"1.0.0.0", "1.0.1.0", "2.0.0.0", "2.0.0.1", "2.0.1.0"

and this is more clear to see what is the idea behind... But, how to convert it into a computer program?? Do any one have any idea on how to sorting this? Thank you.


This isn't a quite solution for the question was asked, but it's very similiar.

This sort function is for semantic versions, it handles resolved version, so it doesn't work with wildcards like x or *.

It works with versions where this regular expression matches: /\d+\.\d+\.\d+.*$/. It's very similar to this answer except that it works also with versions like 1.2.3-dev. In comparison with the other answer: I removed some checks that I don't need, but my solution can be combined with the other one.

semVerSort = function(v1, v2) {
  var v1Array = v1.split('.');
  var v2Array = v2.split('.');
  for (var i=0; i<v1Array.length; ++i) {
    var a = v1Array[i];
    var b = v2Array[i];
    var aInt = parseInt(a, 10);
    var bInt = parseInt(b, 10);
    if (aInt === bInt) {
      var aLex = a.substr((""+aInt).length);
      var bLex = b.substr((""+bInt).length);
      if (aLex === '' && bLex !== '') return 1;
      if (aLex !== '' && bLex === '') return -1;
      if (aLex !== '' && bLex !== '') return aLex > bLex ? 1 : -1;
      continue;
    } else if (aInt > bInt) {
      return 1;
    } else {
      return -1;
    }
  }
  return 0;
}

The merged solution is that:

function versionCompare(v1, v2, options) {
    var zeroExtend = options && options.zeroExtend,
        v1parts = v1.split('.'),
        v2parts = v2.split('.');

    if (zeroExtend) {
        while (v1parts.length < v2parts.length) v1parts.push("0");
        while (v2parts.length < v1parts.length) v2parts.push("0");
    }

    for (var i = 0; i < v1parts.length; ++i) {
        if (v2parts.length == i) {
            return 1;
        }
        var v1Int = parseInt(v1parts[i], 10);
        var v2Int = parseInt(v2parts[i], 10);
        if (v1Int == v2Int) {
            var v1Lex = v1parts[i].substr((""+v1Int).length);
            var v2Lex = v2parts[i].substr((""+v2Int).length);
            if (v1Lex === '' && v2Lex !== '') return 1;
            if (v1Lex !== '' && v2Lex === '') return -1;
            if (v1Lex !== '' && v2Lex !== '') return v1Lex > v2Lex ? 1 : -1;
            continue;
        }
        else if (v1Int > v2Int) {
            return 1;
        }
        else {
            return -1;
        }
    }

    if (v1parts.length != v2parts.length) {
        return -1;
    }

    return 0;
}

The Javascript Function to Compare Version Number Strings , 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 two Version numbers. A version number is a string which is used to identify unique states of a software product. A version number looks like a.b.c.d where a, b etc are number, so version number is a string in which numbers are separated by dots. These number generally represent hierarchy from major to minor (a is major and d is minor).


Here's a version that orders version strings without allocating any sub-strings or arrays. Being that it allocates fewer objects, there's less work for the GC to do.

There is one pair of allocations (to allow reuse of the getVersionPart method), but you could expand that to avoid allocations altogether if you were very performance sensitive.

const compareVersionStrings : (a: string, b: string) => number = (a, b) =>
{
    var ia = {s:a,i:0}, ib = {s:b,i:0};
    while (true)
    {
        var na = getVersionPart(ia), nb = getVersionPart(ib);

        if (na === null && nb === null)
            return 0;
        if (na === null)
            return -1;
        if (nb === null)
            return 1;
        if (na > nb)
            return 1;
        if (na < nb)
            return -1;
    }
};

const zeroCharCode = '0'.charCodeAt(0);

const getVersionPart = (a : {s:string, i:number}) =>
{
    if (a.i >= a.s.length)
        return null;

    var n = 0;
    while (a.i < a.s.length)
    {
        if (a.s[a.i] === '.')
        {
            a.i++;
            break;
        }

        n *= 10;
        n += a.s.charCodeAt(a.i) - zeroCharCode;
        a.i++;
    }
    return n;
}

Checking versions numbers using Javascript, An interesting question popped up on Stackoverflow the other day: How to compare software version number using js? (only number). Given two integer numbers and we have to compare them with each other using java program. Example: Input: Enter first number : 12 Enter second number : 10 Output: 12 != 20 12 20 12 = 20 Program to compare two numbers with each other in java import java. util.


this is my solution. it has accepted on leetcode. I met the problem in my interview today. But i did not solve it at that time. I thought about it again. Adding zeros to make the two arrays' length equal. Then comparison.

var compareVersion = function(version1, version2) {
    let arr1 = version1.split('.').map(Number);
    let arr2 = version2.split('.').map(Number);
    let diff = 0;
    if (arr1.length > arr2.length){
        diff = arr1.length - arr2.length;
        while (diff > 0){
            arr2.push(0);
            diff--;
        } 
    }
    else if (arr1.length < arr2.length){
        diff = arr2.length - arr1.length;
        while (diff > 0){
            arr1.push(0);
            diff--;
        }
    }
   
    let i = 0;
    while (i < arr1.length){
        if (arr1[i] > arr2[i]){
           return 1;
       } else if (arr1[i] < arr2[i]){
           return -1;
       }
        i++;
    }
    return 0;
    
};

Compare two Version numbers, Compare two Version numbers. A version number is a string which is used to identify unique states of a software product. A version number looks like a.b.c.d  Problem Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may


The function will return -1 if the version are equal, 0 if the first version is the latest and 1 to indicate the second version is the latest.

let v1 = '12.0.1.0'
let v2 = '12.0.1'

let temp1 = v1.split('.');
let temp2 = v2.split('.');

console.log(compareVersion(temp1, temp2))


function compareVersion(version1, version2) {
    let flag = false;
    var compareResult;
    let maxLength = Math.max(version1.length, version2.length); 
    let minLength = Math.min(version1.length, version2.length);

    for (let i = 0; i < maxLength; ++i ) {
        let result = version1[i] - version2[i];
        if (result > 0) {
            flag = true;
            compareResult = 0;
            break;
        }
        else if (result < 0) {
            flag = true;
            compareResult = 1;
            break;
        }

        if (i === minLength) {
            if (version1.length > version1.length) {
                compareResult = version1[version1.length-1] > 0 ? '0' : '-1'
            }  else  {
                compareResult = version1[version2.length-1] > 0 ? '1' : '-1'
            }
            break;
        }
    }
    if (flag === false) {
        compareResult = -1;
    }
    return compareResult;
}

compare-versions, Compare semver version strings to find greater, equal or lesser. Runs in the browser as well as Node.js/React Native etc. Has no Supports Chromium version numbers with 4 parts, e.g. version 25.0.1364.126 . Any leading  JavaScript version compare. GitHub Gist: instantly share code, notes, and snippets.


I didn't love any of the solutions so I rewrote it for my coding preferences. Notice the last four checks come out slightly different than the accepted answer. Works for me.

function v_check(version_a, version_b) {
    // compares version_a as it relates to version_b
    // a = b => "same"
    // a > b => "larger"
    // a < b => "smaller"
    // NaN   => "invalid"

    const arr_a = version_a.split('.');
    const arr_b = version_b.split('.');

    let result = "same"; // initialize to same // loop tries to disprove

    // loop through a and check each number against the same position in b
    for (let i = 0; i < arr_a.length; i++) {
        let a = arr_a[i];
        let b = arr_b[i];

        // same up to this point so if a is not there, a is smaller
        if (typeof a === 'undefined') {
            result = "smaller";
            break;

        // same up to this point so if b is not there, a is larger
        } else if (typeof b === 'undefined') {
            result = "larger";
            break;

        // otherwise, compare the two numbers
        } else {

            // non-positive numbers are invalid
            if (a >= 0 && b >= 0) {

                if (a < b) {
                    result = "smaller";
                    break;
                }
                else if (a > b) {
                    result = "larger";
                    break;
                }

            } else {
                result = "invalid";
                break;
            }
        }
    }

    // account for the case where the loop ended but there was still a position in b to evaluate
    if (result == "same" && arr_b.length > arr_a.length) result = "smaller";

    return result;
}


console.log(v_check("1.7.1", "1.7.10"));  // smaller
console.log(v_check("1.6.1", "1.7.10"));  // smaller
console.log(v_check("1.6.20", "1.7.10")); // smaller
console.log(v_check("1.7.1", "1.7.10"));  // smaller
console.log(v_check("1.7", "1.7.0"));     // smaller
console.log(v_check("1.7", "1.8.0"));     // smaller

console.log(v_check("1.7.10", "1.7.1"));  // larger
console.log(v_check("1.7.10", "1.6.1"));  // larger
console.log(v_check("1.7.10", "1.6.20")); // larger
console.log(v_check("1.7.0", "1.7"));     // larger
console.log(v_check("1.8.0", "1.7"));     // larger

console.log(v_check("1.7.10", "1.7.10")); // same
console.log(v_check("1.7", "1.7"));       // same

console.log(v_check("1.7", "1..7")); // larger
console.log(v_check("1.7", "Bad"));  // invalid
console.log(v_check("1..7", "1.7")); // smaller
console.log(v_check("Bad", "1.7"));  // invalid

Comparing version numbers with JavaScript, A small review;. Once you know that one version digit is larger than the other, you can exit immediately; You did not declare i with const or let  If for some reason you are not allowed to use the compare-method of the Version directly (e.g. in a client-server scenario), another approach is to extract a long number from the version and then compare the numbers with each other. However, the number needs to have the following format: Two digits for Major, Minor and Revision and four for Build.


JavaScript version compare · GitHub, Simply compares two string version values. And FALSE if one of input versions are not valid @param {String} right Version #2 tests.qunit.js '3000'; lib said '​5' > '3000'. seems just comparing with sting. after add parseInt() in for statment  "Version numbers" are a matter for your internal version control system. Release numbers are a different matter (and should be KEPT different). Stick to a simple MAJOR.MINOR release system (like v1.27), where MAJOR is the compatibility level (version 2.x is incompatible with or at least majorly different from version 1.x) and MINOR is your


Function to compare two version strings (e.g. "1.6.1" is smaller than , version_compare.js. /**. * Compares two software version numbers (e.g. "1.7.1" or "1.2b"). @param {string} v2 The second version to be compared. * @param  Software upgrade versioning is the process of assigning either unique version names or unique version numbers to unique states of computer software.Within a given version number category (major, minor), these numbers are generally assigned in increasing order and correspond to new developments in the software.


Software versioning, Software upgrade versioning is the process of assigning either unique version names or unique version numbers to unique states of computer software. Within a  How to compare two Value in JavaScipts: 1.Get the Value From Html Page using getElementById. 2.Compare by Using parseInt For Interger Value and parseFloat For Float Value. 3.compare two value as. 4.Include the code in Html Page For TextBox.