叮当猫咪
使用自定义比较器function sorta(a, b) { let [numA, decA] = a[0].split("."); let [numB, decB] = b[0].split("."); if (numA != numB) { return numA - numB; } else { return decA-decB }} arr = [ ['1','lorem'], ['2','ipsum'], ['1.1','sit'], ['1.2','lorem'], ['3','ipsum'], ['1.3','sit'], ['1.4','sit'], ['1.5','lorem'], ['4','ipsum'], ['1.6','sit'], ['1.7','sit'], ['1.8','sit'], ['1.9','lorem'], ['5','ipsum'], ['1.10','sit']]console.log(arr.sort(sorta))
守候你守候我
根据 '.' 拆分字符串 然后分别比较每个部分,如下所示 -var arr = [ ['1','lorem'], ['2','ipsum'], ['1.1','sit'], ['1.2','lorem'], ['3','ipsum'], ['1.3','sit'], ['1.4','sit'], ['1.5','lorem'], ['4','ipsum'], ['1.6','sit'], ['1.7','sit'], ['1.8','sit'], ['1.9','lorem'], ['5','ipsum'], ['1.10','sit']]var sortedArr=arr.sort((a,b)=>{ var aArr=a[0].split('.'); var bArr=b[0].split('.'); if(aArr[0]!==bArr[0]) { return aArr[0]-bArr[0]; } else { if(aArr.length===1) { return -1 } if(bArr.length===1) { return 1; } return parseInt(aArr[1])-parseInt(bArr[1]); }})console.log(JSON.stringify(sortedArr));// [["1","lorem"],["1.1","sit"],["1.2","lorem"],["1.3","sit"],["1.4","sit"],["1.5","lorem"],["1.6","sit"],["1.7","sit"],["1.8","sit"],["1.9","lorem"],["1.10","sit"],["2","ipsum"],["3","ipsum"],["4","ipsum"],["5","ipsum"]]