【发布时间】:2014-02-25 10:02:27
【问题描述】:
我有以下来自 facebook graph API 的数组。 我想在javascript中按comment_count、like_count、时间对其进行排序。
[
{
"status_id": "1",
"message": "message1",
"comment_info": {
"comment_count": "1"
},
"like_info": {
"like_count": "0"
},
"time": "1380046653"
},
{
"status_id": "2",
"message": "message2",
"comment_info": {
"comment_count": "2"
},
"like_info": {
"like_count": "5"
},
"time": "1368109884"
}
] 我写了如下函数,
function sortResults(prop, asc) {
statusString = statusString.sort(function(a, b) {
if (asc) return (a[prop] > b[prop]) ? 1 : ((a[prop] < b[prop]) ? -1 : 0);
else return (b[prop] > a[prop]) ? 1 : ((b[prop] < a[prop]) ? -1 : 0);
});
console.log(statusString);
}
然后点击按钮
sortResults(['comment_info']['comment_count'], true);
但它的排序很奇怪。
【问题讨论】:
-
你明白
['comment_info']['comment_count']在做什么吗?您对该代码的期望是什么? -
而且您的要求还不清楚。您想按其中的 一个 属性排序吗?或者您想在找到相等时按这些属性进行子排序?
标签: javascript json sorting facebook-graph-api multidimensional-array