以下是软件版本号:

"1.0", "1.0.1", "2.0", "2.0.0.1", "2.0.1"

我怎么比较呢?

假设正确的顺序是:

"1.0", "1.0.1", "2.0", "2.0.0.1", "2.0.1"

想法很简单… 读第一个数字,然后,第二个,第三个… 但是我不能将版本号转换为浮点数… 你也可以像这样看到版本号:

"1.0.0.0", "1.0.1.0", "2.0.0.0", "2.0.0.1", "2.0.1.0"

这样可以更清楚地看到背后的想法。 但是,我怎样才能把它转换成计算机程序呢?


当前回答

你可以使用JavaScript的localeCompare方法:

a.localeCompare(b, undefined, {numeric: true})

这里有一个例子:

"1.1".localeCompare("2.1.1", undefined, {numeric: true}) => -1

"1.0.0".localeCompare("1.0", undefined, {numeric: true}) =>

"1.0.0".localeCompare("1.0.0", undefined, {numeric: true}) => 0

其他回答

以下是我的解决方案,适用于任何深度的任何版本。

自动处理数字+点问题。如果不是这样,函数存在,控制台日志将给出undefined而不是true, false或true。

自动处理尾随零问题。

任何可能的地方都存在自动继电器。

自动向后兼容旧浏览器。

function checkVersion (vv,vvv){ if(!(/^[0-9.]*$/.test(vv) && /^[0-9.]*$/.test(vvv))) return; va = vv.toString().split('.'); vb = vvv.toString().split('.'); length = Math.max(va.length, vb.length); for (i = 0; i < length; i++) { if ((va[i]|| 0) < (vb[i]|| 0) ) {return false; } } return true;} console.log(checkVersion('20.0.0.1' , '20.0.0.2')); console.log(checkVersion(20.0 , '20.0.0.2')); console.log(checkVersion('20.0.0.0.0' , 20)); console.log(checkVersion('20.0.0.0.1' , 20)); console.log(checkVersion('20.0.0-0.1' , 20));

// Returns true if v1 is bigger than v2, and false if otherwise.
function isNewerThan(v1, v2) {
      v1=v1.split('.');
      v2=v2.split('.');
      for(var i = 0; i<Math.max(v1.length,v2.length); i++){
        if(v1[i] == undefined) return false; // If there is no digit, v2 is automatically bigger
        if(v2[i] == undefined) return true; // if there is no digit, v1 is automatically bigger
        if(v1[i] > v2[i]) return true;
        if(v1[i] < v2[i]) return false;
      }
      return false; // Returns false if they are equal
    }

我想为解决这个问题的轻量级库做广告:语义版本

可以同时使用面向对象(OO)和面向函数。它可以作为npm-package和现成的包文件使用。

我也遇到过类似的问题,而且我已经为它创建了一个解决方案。你可以试一试。

如果等于则返回0,如果版本号大于则返回1,如果版本号小于则返回-1

function compareVersion(currentVersion, minVersion) { let current = currentVersion.replace(/\./g," .").split(' ').map(x=>parseFloat(x,10)) let min = minVersion.replace(/\./g," .").split(' ').map(x=>parseFloat(x,10)) for(let i = 0; i < Math.max(current.length, min.length); i++) { if((current[i] || 0) < (min[i] || 0)) { return -1 } else if ((current[i] || 0) > (min[i] || 0)) { return 1 } } return 0 } console.log(compareVersion("81.0.1212.121","80.4.1121.121")); console.log(compareVersion("81.0.1212.121","80.4.9921.121")); console.log(compareVersion("80.0.1212.121","80.4.9921.121")); console.log(compareVersion("4.4.0","4.4.1")); console.log(compareVersion("5.24","5.2")); console.log(compareVersion("4.1","4.1.2")); console.log(compareVersion("4.1.2","4.1")); console.log(compareVersion("4.4.4.4","4.4.4.4.4")); console.log(compareVersion("4.4.4.4.4.4","4.4.4.4.4")); console.log(compareVersion("0","1")); console.log(compareVersion("1","1")); console.log(compareVersion("1","1.0.00000.0000")); console.log(compareVersion("","1")); console.log(compareVersion("10.0.1","10.1"));

你可以使用带有选项的String#localeCompare

sensitivity Which differences in the strings should lead to non-zero result values. Possible values are: "base": Only strings that differ in base letters compare as unequal. Examples: a ≠ b, a = á, a = A. "accent": Only strings that differ in base letters or accents and other diacritic marks compare as unequal. Examples: a ≠ b, a ≠ á, a = A. "case": Only strings that differ in base letters or case compare as unequal. Examples: a ≠ b, a = á, a ≠ A. "variant": Strings that differ in base letters, accents and other diacritic marks, or case compare as unequal. Other differences may also be taken into consideration. Examples: a ≠ b, a ≠ á, a ≠ A. The default is "variant" for usage "sort"; it's locale dependent for usage "search". numeric Whether numeric collation should be used, such that "1" < "2" < "10". Possible values are true and false; the default is false. This option can be set through an options property or through a Unicode extension key; if both are provided, the options property takes precedence. Implementations are not required to support this property.

var版本=[" 2.0.1”、“2.0”、“1.0”、“1.0.1”,“2.0.0.1”); 版本。sort((a, b) => a.localeCompare(b, undefined, {numeric: true,灵敏度:'base'})); console.log(版本);