以下是软件版本号:
"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"
这样可以更清楚地看到背后的想法。
但是,我怎样才能把它转换成计算机程序呢?
你不能把它们转换成数字,然后按大小排序吗?在长度< 4的数的1后面加上0
在主机上玩:
$(["1.0.0.0", "1.0.1.0", "2.0.0.0", "2.0.0.1", "2.0.1", "3.0"]).each(function(i,e) {
var n = e.replace(/\./g,"");
while(n.length < 4) n+="0" ;
num.push( +n )
});
版本越大,数字越大。
编辑:可能需要调整,以考虑更大的版本系列
以下是我的解决方案,适用于任何深度的任何版本。
自动处理数字+点问题。如果不是这样,函数存在,控制台日志将给出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));
你可以使用带有选项的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))
这个非常小,但非常快的比较函数接受每个段的任何长度和任何数字大小的版本号。
返回值:
-如果a < b,则数字< 0
-如果是> b,则为> 0
如果a = b - 0
所以你可以使用它作为array。sort()的比较函数;
编辑:修正了版本剥离尾随零识别“1”和“1.0.0”相等的错误
function cmpVersions (a, b) {
var i, diff;
var regExStrip0 = /(\.0+)+$/;
var segmentsA = a.replace(regExStrip0, '').split('.');
var segmentsB = b.replace(regExStrip0, '').split('.');
var l = Math.min(segmentsA.length, segmentsB.length);
for (i = 0; i < l; i++) {
diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10);
if (diff) {
return diff;
}
}
return segmentsA.length - segmentsB.length;
}
// TEST
console.log(
['2.5.10.4159',
'1.0.0',
'0.5',
'0.4.1',
'1',
'1.1',
'0.0.0',
'2.5.0',
'2',
'0.0',
'2.5.10',
'10.5',
'1.25.4',
'1.2.15'].sort(cmpVersions));
// Result:
// ["0.0.0", "0.0", "0.4.1", "0.5", "1.0.0", "1", "1.1", "1.2.15", "1.25.4", "2", "2.5.0", "2.5.10", "2.5.10.4159", "10.5"]
我也遇到过类似的问题,而且我已经为它创建了一个解决方案。你可以试一试。
如果等于则返回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"));