js比较版本号

function versionStringCompare (prerVersion=‘‘, lastVersion=‘‘){
  var sources = prerVersion.split(‘.‘);
  var dests = lastVersion.split(‘.‘);
  var maxL = Math.max(sources.length, dests.length);
  var result = 0;
  for (let i = 0; i < maxL; i++) {  
      let preValue = sources.length>i ? sources[i]:0;
      let preNum = isNaN(Number(preValue)) ? preValue.charCodeAt() : Number(preValue);
      let lastValue = dests.length>i ? dests[i]:0;
      let lastNum =  isNaN(Number(lastValue)) ? lastValue.charCodeAt() : Number(lastValue);
      if (preNum < lastNum) {
          result = -1;
          break;
      } else if (preNum > lastNum) { 
          result = 1;
          break;
      }
  }
  return result;
}

console.log(versionStringCompare(‘1.0.1.211‘,‘1.0.1.2‘));//1
console.log(versionStringCompare(‘1.0.1.0‘,‘1.0.1‘));//0
console.log(versionStringCompare(‘1.0.1.1‘,‘1.0.1.2‘));//-1
console.log(versionStringCompare(‘1‘,‘2‘));//-1

 

 

 

 

 

js比较版本号

上一篇:头条二面:详述一条 SQL 的执行流程


下一篇:DRF: drf_yasg中将Schemes从HTTP更改为HTTPS