javascript 数组排序不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23300682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array Sort not working
提问by Mdd
I have an array of objects that I am trying to sort but it does not seem to be working. Some objects in the array have a orderNum
property which I am targeting to sort. But not all objects have this property.
我有一组我正在尝试排序的对象,但它似乎不起作用。数组中的某些对象具有orderNum
我要排序的属性。但并非所有对象都具有此属性。
I want the objects with the orderNum
property to be sorted to the top positions in the array.
我希望将具有该orderNum
属性的对象排序到数组中的顶部位置。
Here is a fiddle to what i have tried: http://jsfiddle.net/7D8sN/
这是我尝试过的小提琴:http: //jsfiddle.net/7D8sN/
Here is my javascript:
这是我的javascript:
var data = {
"attributes": [
{
"value": "123-456-7890",
"name": "phone"
},
{
"value": "[email protected]",
"name": "email"
},
{
"value": "Gotham",
"name": "city",
"orderNum": 1
},
{
"value": "12",
"name": "ID"
},
{
"value": "Batman",
"name": "Super Hero",
"orderNum": 2
}
]
};
data.attributes.sort( function (a, b) {
if (a.orderNum < b.orderNum) {
return -1;
}
if (a.orderNum > b.orderNum) {
return 1;
}
return 0;
});
console.log(data);
回答by Jason Harwig
Check if the property exists in your sort function.
检查该属性是否存在于您的排序函数中。
data.attributes.sort( function (a, b) {
if ((typeof b.orderNum === 'undefined' && typeof a.orderNum !== 'undefined') || a.orderNum < b.orderNum) {
return -1;
}
if ((typeof a.orderNum === 'undefined' && typeof b.orderNum !== 'undefined') || a.orderNum > b.orderNum) {
return 1;
}
return 0;
});
回答by Barmar
You have to check specifically for the property being undefined
. Otherwise, both tests return false
, so you fall through to return 0
and treat them as equal to everything.
您必须专门检查属性为undefined
。否则,两个测试都返回false
,因此您会失败return 0
并将它们视为等同于一切。
data.attributes.sort( function (a, b) {
if (a.orderNum === undefined || a.orderNum < b.orderNum) {
return -1;
}
if (b.orderNum === undefined || b.orderNum < a.orderNum) {
return 1;
}
return 0;
});
回答by Danny
You can check whether each object has the property with hasOwnProperty("orderNum")
and then sort them accordingly. If one has it, and the other does not, then the one with it gets put first. I made the assumption that you were sorting with orderNum ascending.
您可以检查每个对象是否具有属性,hasOwnProperty("orderNum")
然后相应地对它们进行排序。如果一个拥有它,而另一个没有,那么拥有它的将被放在第一位。我假设您是按 orderNum 升序排序的。
JSFiddle: http://jsfiddle.net/dshell/RFr5N/
JSFiddle:http: //jsfiddle.net/dshell/RFr5N/
data.attributes.sort( function (a, b) {
if ((a.hasOwnProperty("orderNum")) && (b.hasOwnProperty("orderNum")))
{
return a.orderNum - b.orderNum;
}
else if (a.hasOwnProperty("orderNum"))
{
return -1;
}
else if (b.hasOwnProperty("orderNum"))
{
return 1;
}
return 0;
});
回答by GameAlchemist
What you need is to 'normalize' your input :
您需要的是“标准化”您的输入:
data.attributes.sort( function (a, b) {
var aOrderNum = ( a.orderNum === undefined ) ? -1 : a.orderNum ;
var bOrderNum = ( b.orderNum === undefined ) ? -1 : b.orderNum ;
return aOrderNum - bOderNum;
});