以下是软件版本号:

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

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


当前回答

原谅我,如果这个想法已经访问在一个链接我没有看到。

我已经有了一些成功的部分转换成一个加权和像这样:

partSum = this.major * Math.Pow(10,9);
partSum += this.minor * Math.Pow(10, 6);
partSum += this.revision * Math.Pow(10, 3);
partSum += this.build * Math.Pow(10, 0);

这使得比较非常容易(比较double)。 我们的版本字段永远不会超过4位数字。

7.10.2.184  -> 7010002184.0
7.11.0.1385 -> 7011001385.0

我希望这能帮助到一些人,因为多重条件句似乎有点过度。

其他回答

检查php.js项目中的version_compare()函数。它类似于PHP的version_compare()。

你可以像这样简单地使用它:

version_compare('2.0', '2.0.0.1', '<'); 
// returns true

你可以使用带有选项的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(版本);

现在我们可以使用Intl了。Collator API现在创建数值比较器。浏览器支持是相当不错的,但在撰写本文时,Node.js还不支持。

const semverCompare = new Intl。Collator("en", {numeric: true}).compare; const版本=[1.0.1,“1.10.2”,“1.1.1”,‘1.10.1’,‘1.5.10’,‘2.10.0’,‘2.0.1’); console.log (versions.sort (semverCompare)) const example2 =(“1.0”,“1.0.1”,“2.0”,“2.0.0.1”,“2.0.1”); console.log (example2.sort (semverCompare))

我找到了最简单的方法来比较它们,但我不确定这是否是你想要的。

当我在控制台中运行下面的代码时,它是有意义的,并且使用sort()方法,我可以获得版本字符串的排序数组。它是根据字母顺序排列的。

"1.0" < "1.0.1" //true
var arr = ["1.0.1", "1.0", "3.2.0", "1.3"]
arr.sort();     //["1.0", "1.0.1", "1.3", "3.2.0"]

这就是我的解。它已经在leetcode上被接受。我在今天的面试中遇到了问题。但我当时没有解决它。 我又想了想。加0使两个数组的长度相等。然后比较。

var compareVersion = function(version1, version2) { let arr1 = version1.split('.').map(Number); let arr2 = version2.split('.').map(Number); let diff = 0; if (arr1.length > arr2.length){ diff = arr1.length - arr2.length; while (diff > 0){ arr2.push(0); diff--; } } else if (arr1.length < arr2.length){ diff = arr2.length - arr1.length; while (diff > 0){ arr1.push(0); diff--; } } let i = 0; while (i < arr1.length){ if (arr1[i] > arr2[i]){ return 1; } else if (arr1[i] < arr2[i]){ return -1; } i++; } return 0; };