问题:
封装一个函数进行版本号排序处理
思路:
1.使用数组自带sort方法;
2.需要将sort里面的只以点分割成数组;
3.判断数组长度,进行排序;
4.如果数组长度相同,在进行循环,两个数组进行比较排序;
5.最后返回结果;
代码:
const versions = ['1.2.9', '1.1', '2.1', '1.3', '2.2','2121.121.1121.112'];function getMaxVersion(version) {return version.sort((a, b) => {const a1 = a.split('.');const b1 = b.split('.');if (a1.length > b1.length) {return -1;} else if (a1.length == b1.length) {for (let i = 0; i < a1.length || b1.length; i++) {if (a1[i]==undefined) {return -1}if (b[i]==undefined) {return 1}if (a1[i] !== b1[i]) {return b1[i] - a1[i]}}}return 0 })
}
console.log(getMaxVersion(versions));