3 回答
TA贡献1770条经验 获得超3个赞
var arr=['0.1.1', '2.3.3', '0.3002.1', '4.2', '4.3.5', '4.3.4.5'];
arr.sort(function(a,b){
return a>b?-1:1;
});
console.log(arr);
TA贡献1875条经验 获得超5个赞
var arr = ['0.1.1', '2.3.3', '0.3002.1', '4.2', '4.3.5', '4.3.4.5']
arr.sort((a,b)=>{
var items1 = a.split('.')
var items2 = b.split('.')
var k = 0
for (let i in items1) {
let a1 = items1[i]
let b1 = items2[i]
if (typeof a1 === undefined) {
k = -1
break
} else if (typeof b1 === undefined) {
k = 1
break
} else {
if (a1 === b1) {
continue
}
k = Number(a1) - Number(b1)
break
}
}
return k
})
console.log(arr)
TA贡献1842条经验 获得超21个赞
// 使用的是选择排序
const versionSort = version => {
const temp = version.map(v => v.split('.'));
for (let i = 0; i < temp.length; i++) {
let minIndex = i;
for (let j = i; j < temp.length; j++) {
for (let k = 0; k < temp[j].length; k++) {
const current = +temp[j][k],
min = +temp[minIndex][k];
if (current < min) {
minIndex = j;
}
// 只要不等,就立刻结束最内层遍历!
if (current !== min) {
break
}
}
}
[temp[i], temp[minIndex]] = [temp[minIndex], temp[i]];
}
return temp.map(v = > v.join('.'))
};
添加回答
举报