。
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
。