-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathres.js
37 lines (32 loc) · 1.02 KB
/
res.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* res.js
* @authors Joe Jiang ([email protected])
* @date 2017-05-11 13:49:44
*
* @param {string} version1
* @param {string} version2
* @return {number}
*/
let compareVersion = function(version1, version2) {
let v1array = version1.split('.'),
v2array = version2.split('.'),
exp = new RegExp('^0+$');
while(exp.exec(v1array[v1array.length-1])) v1array.pop();
while(exp.exec(v2array[v2array.length-1])) v2array.pop();
// while(v1array[v1array.length-1].match(exp)) v1array.pop();
// while(v2array[v2array.length-1].match(exp)) v2array.pop();
let v1len = v1array.length,
v2len = v2array.length,
minlen = Math.min(v1len, v2len);
for (let i=0; i<minlen; i++) {
let num1 = Number.parseInt(v1array[i]),
num2 = Number.parseInt(v2array[i]);
if (num1>num2) return 1;
else if (num1<num2) return -1;
}
if (v1len === v2len) {
return 0;
} else {
return v1len>v2len? 1:-1;
}
};