title: Javascript function to compare two versions
body: <p>Im writting a function to compare 2 versions, and return true if the second is bigger then first version.</p>
  
  <p>but my algorithm have a "hole", and I cant figure out how fix.</p>
  
  <pre><code>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&lt;(Math.max(version1.length,version2.length));i++){
  
          if(version1[i]==undefined){ version1[i]=0; }
          if(version2[i]==undefined){ version2[i]=0; }
  
          if(version1[i]&lt;version2[i]){
              result=true;
              break;
          }
      }
      return(result);
  }
  </code></pre>
  
  <p>this exemples return as expected</p>
  
  <pre><code>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
  </code></pre>
  
  <p>but this </p>
  
  <pre><code>compareversion('1.1.0','1.0.1') //return true but should be false
  </code></pre>