我有这个数组:
["4.1.0.57", "4.1.0.99", "4.1.0.54", "4.1.0.65", "4.1.0.87", "4.1.0.97", "4.1.0.63", "4.1.0.96", "4.1.0.51", "4.1.0.84", "4.1.0.95", "4.1.0.105", "4.1.0.38", "4.1.0.59", "4.1.0.47", "4.1.0.69", "4.1.0.93", "4.1.0.92", "4.1.0.103", "4.1.0.100", "4.1.0.90"]
我正在尝试使用 lodash 对其进行排序:
_.sortBy(keys)
_.orderBy(keys)
["4.1.0.100", "4.1.0.103", "4.1.0.105", "4.1.0.38", "4.1.0.47", "4.1.0.51", "4.1.0.54", "4.1.0.57", "4.1.0.59", "4.1.0.63", "4.1.0.65", "4.1.0.69", "4.1.0.84", "4.1.0.87", "4.1.0.90", "4.1. 0.92", "4.1.0.93", "4.1.0.95", "4.1.0.96", "4.1.0.97", "4.1.0.99"]
排序不正确
最佳答案
您可以使用 String#localeCompare
进行排序和选项。
var array = ["4.1.0.57", "4.1.0.99", "4.1.0.54", "4.1.0.65", "4.1.0.87", "4.1.0.97", "4.1.0.63", "4.1.0.96", "4.1.0.51", "4.1.0.84", "4.1.0.95", "4.1.0.105", "4.1.0.38", "4.1.0.59", "4.1.0.47", "4.1.0.69", "4.1.0.93", "4.1.0.92", "4.1.0.103", "4.1.0.100", "4.1.0.90"];
array.sort(function (a,b) {
return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' });
});
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
关于javascript - 在javascript中订购带有多个点的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47458541/