Javascript function to compare two versions

Im calls a function to compare two versions and returns true if the second is larger than the first version.

but my algorithm has a β€œhole” and I cannot figure out how to fix it.

function compareversion(version1,version2){ var result=false; if(typeof version1!=='object'){ version1=version1.toString().split('.'); } if(typeof version2!=='object'){ version2=version2.toString().split('.'); } for(var i=0;i<(Math.max(version1.length,version2.length));i++){ if(version1[i]==undefined){ version1[i]=0; } if(version2[i]==undefined){ version2[i]=0; } if(version1[i]<version2[i]){ result=true; break; } } return(result); } 

this example returns as expected

 compareversion('1','1') //false compareversion('1','1.0.0') //false compareversion('2.0','1.0.0') //false compareversion('1.1','1.2') //true compareversion('1','1.0.0.1') //true 

but this one

 compareversion('1.1.0','1.0.1') //return true but should be false 
+4
source share
5 answers

This should work:

 function compareversion(version1,version2){ var result=false; if(typeof version1!=='object'){ version1=version1.toString().split('.'); } if(typeof version2!=='object'){ version2=version2.toString().split('.'); } for(var i=0;i<(Math.max(version1.length,version2.length));i++){ if(version1[i]==undefined){ version1[i]=0; } if(version2[i]==undefined){ version2[i]=0; } if(Number(version1[i])<Number(version2[i])){ result=true; break; } if(version1[i]!=version2[i]){ break; } } return(result); } 

The reason compareversion('1.1.0','1.0.1') does not correspond to the fact that your code first compares 1 with 1 , then 1 to 0 (it does not interrupt here, since it only breaks if version1[i] < version2[i] ), and then 0 to 1 .

Since 0 < 1 , it returns false .

+7
source

If version1 is in this index anymore, you know that it should return false. You need to continue if they are equal.

  if(version1[i]<version2[i]){ result=true; break; } if(version1[i]>version2[i]){ result=false; break; } // Will only get here if both are equal, in all other // cases you broke out of the loop. So only continue // checking the next index when this one was equal. 
+4
source

Your problem is that if you have 1.1.0 and 1.0.1, the function does not reach the break when you need it so that

 if(version1[i]<version2[i]){ result=true; break; } 

he continues to compare numbers to the end.

you get 1 <1 == false, if does not start

then you will get 1 <0 == false, if does not start

and then 0 <1 == true if result = true does. you will need:

 if(version1[i]<version2[i]){ return true; }else if(version1[i]>version2[i]){ return false; } 
+2
source

None of these answers are optimal for the following reasons:

  • Your passed parameters can be strings. later you convert them to an array. it's expensive and goes against good coding practice. NEVER change the type of a variable after it is initialized! Create a new variable to store the array.
  • A function continues through the loop even after it has enough information to return with the result.

Basically, you want to start with the main version and work up to the younger version. Once you find one that is not equal, you want to return. It is standard when performing a function (a, b) that is greater than the operation that the return value of the function should be:

 1 = a > b -1 = a < b 0 = a===b 

Otherwise, anyone calling this function will not know why it returns true, unless it looks at its internal elements. If it is reduced and confused, it can be a waste of time.

I rewrote and improved the function with this in mind.

 function (versionA, versionB) { var arrA, arrB; arrA = typeof versionA !== 'object' ? versionA.toString().split('.') : versionA; arrB = typeof versionB !== 'object' ? versionB.toString().split('.') : versionB; for (var i = 0; i < (Math.max(arrA.length, arrB.length)); i++) { arrA[i] = typeof arrA[i] === 'undefined' ? 0 : Number(arrA[i]); arrB[i] = typeof arrB[i] === 'undefined' ? 0 : Number(arrB[i]); if (arrA[i] > arrB[i]) { return 1; } if (arrA[i] < arrB[i]) { return -1; } } return 0; }; 
+1
source
 version1 = version1.toString(); version2 = version2.toString(); var matchFound = false; if(version1.length != version2.length){ //Different Versions }else{ //They are the same length so compare each element FIND: for(var i = 0; i < version1.length; i++){ var match = version[i].match(version2[i]){ if(match == ""){ //Match found matchFound = true; break FIND; } } return matchFound; 
0
source

All Articles