在javascript

时间:2017-11-23 15:08:31

标签: javascript lodash

我有这个数组:

["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"]

它没有正确订购

1 个答案:

答案 0 :(得分:5)

您可以使用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; }