var points = [2, a, t, 5, 4, 3]; 排序后: var points = [2, a, t, 3, 4, 5];
有什么帮助吗?因为我在 Adobe DC 项目上工作,所以可以使用旧式 javascript 代码发布您的帮助?
我的代码在那里:
var points = [t1, t2, t3, t4];
[t1, t2, t3, t4] = points;
var lucky = points.filter(function(number) {
return isNaN(number) == false && number !=="" && number!== undefined;
});
points=lucky
points.sort(function(a, b){return a - b});
this.getField("Text6").value = points
但这只会从最小值到最大值排序并过滤其他字符...我需要保留其他字符和简短的数字...
最佳答案
var points = [2, "a", "t", 5, 4, 11, "", 3];
var insert = points.slice(0); // Clone points
// Remove all elements not a number
points = points.filter(function(element) {
return typeof element === 'number';
});
// Sort the array with only numbers
points.sort(function(a, b) {
return a - b;
});
// Re-insert all non-number elements
insert.forEach(function(element, index) {
if (typeof element !== 'number') {
points.splice(index, 0, element);
}
});
console.log(points);
关于javascript - 如何仅对数组中的数值进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54689198/