-2

I have tried to compare the below strings. but I'm not able to get the correct answer.

    var a = "17.4.0.50";
    var b = "3.1.0.114";
    
    if(a>b){
    alert("a is greater than b");
    }
    else{
    alert("b is greater than a");
    }

Got result as "b is greater than a".

Could anyone help me to get correct result on this?

Thanks in advance,

smunteanu
  • 422
  • 3
  • 9

1 Answers1

0

It appears that you're trying to compare versions. This is what I'd suggest

//Need to compute the common minimum patch version place
var numPatchIdentifiersArray = new Array();
var aPatches = a.split(".")
var bPatches = b.split(".")

numPatchIdentifiersArray.push(aPatches.length);
numPatchIdentifiersArray.push(bPatches.length);

var minNumPatchIdentifiers = min(numDecimalsArray);

for (var index = 0; index < minNumPatchIdentifiers; index++) {
    if (parseInt(aPatches[index]) > parseInt(bPatches[index])) {
        console.log("a > b");
        break;
    } else if (parseInt(aPatches[index]) < parseInt(bPatches[index])) {
        console.log("a < b");
        break;
    } 
}
dipanjanb
  • 51
  • 3