Javascript—按嵌套值对JSON对象排序

Javascript—按嵌套值对JSON对象排序,javascript,jquery,json,sorting,Javascript,Jquery,Json,Sorting,总结:有一个多级父子评论系统 示例: comment 1 comment 1-1 comment 1-1-1 comment 1-2 comment 1-2-1 comment 1-2-2 { "CommentText": "", "CommentCreateStamp": "2016-03-22", "Comments": [{ "CommentText": "comment 1", "Co

总结:有一个多级父子评论系统

示例:

comment 1
    comment 1-1
        comment 1-1-1
    comment 1-2
        comment 1-2-1
             comment 1-2-2
{
  "CommentText": "",
  "CommentCreateStamp": "2016-03-22",
  "Comments": [{
    "CommentText": "comment 1",
    "CommentCreateStamp": "2016-03-09",
    "Comments": [{
      "CommentText": "comment 1-1",
      "CommentCreateStamp": "2016-03-15",
      "Comments": [{
        "CommentText": "comment 1-1-1",
        "CommentCreateStamp": "2016-03-21",
        "Comments": null
      }]
    }]
  }],
  "Comments": [{
    "CommentText": "comment 2",
    "CommentCreateStamp": "2016-03-12",
    "Comments": [{
      "CommentText": "comment 2-1",
      "CommentCreateStamp": "2016-03-10",
      "Comments": [{
        "CommentText": "comment 2-1-1",
        "CommentCreateStamp": "2016-03-14",
        "Comments": null
      }]
    }]
  }]
}
这里,
注释1
是父项,
注释1-1
注释1-2
都是
注释1
的子项,依此类推

要求:

我们希望实现基于
属性值的整个对象的
排序
<代码>时间戳
(最新评论或回复将出现在顶部)

JSON对象:

comment 1
    comment 1-1
        comment 1-1-1
    comment 1-2
        comment 1-2-1
             comment 1-2-2
{
  "CommentText": "",
  "CommentCreateStamp": "2016-03-22",
  "Comments": [{
    "CommentText": "comment 1",
    "CommentCreateStamp": "2016-03-09",
    "Comments": [{
      "CommentText": "comment 1-1",
      "CommentCreateStamp": "2016-03-15",
      "Comments": [{
        "CommentText": "comment 1-1-1",
        "CommentCreateStamp": "2016-03-21",
        "Comments": null
      }]
    }]
  }],
  "Comments": [{
    "CommentText": "comment 2",
    "CommentCreateStamp": "2016-03-12",
    "Comments": [{
      "CommentText": "comment 2-1",
      "CommentCreateStamp": "2016-03-10",
      "Comments": [{
        "CommentText": "comment 2-1-1",
        "CommentCreateStamp": "2016-03-14",
        "Comments": null
      }]
    }]
  }]
}
我已经试过了:

comment 1
    comment 1-1
        comment 1-1-1
    comment 1-2
        comment 1-2-1
             comment 1-2-2
{
  "CommentText": "",
  "CommentCreateStamp": "2016-03-22",
  "Comments": [{
    "CommentText": "comment 1",
    "CommentCreateStamp": "2016-03-09",
    "Comments": [{
      "CommentText": "comment 1-1",
      "CommentCreateStamp": "2016-03-15",
      "Comments": [{
        "CommentText": "comment 1-1-1",
        "CommentCreateStamp": "2016-03-21",
        "Comments": null
      }]
    }]
  }],
  "Comments": [{
    "CommentText": "comment 2",
    "CommentCreateStamp": "2016-03-12",
    "Comments": [{
      "CommentText": "comment 2-1",
      "CommentCreateStamp": "2016-03-10",
      "Comments": [{
        "CommentText": "comment 2-1-1",
        "CommentCreateStamp": "2016-03-14",
        "Comments": null
      }]
    }]
  }]
}
JSfiddle:

我看到很多关于stackoverflow的问题,但没有按照要求工作


任何直接的帮助都是非常值得的。谢谢

我不完全确定您在问什么,但您的代码似乎有两个问题:

  • 您重新定义了对象的
    comments
    属性,也许您想要一个数组
  • 排序函数应该返回基于元素比较的值,而不是输出它。有关日期比较,请参阅

更新:很接近,缺少的是对嵌套注释的
排序的调用。
下面是一个递归调用
排序
的代码段:

var-people={
“注释文本”:“,
“CommentCreateStamp”:“,
“评论”:[{
“注释文本”:“c1”,
“CommentCreateStamp”:“2016-03-23 06:05:36”,
“评论”:[{
“评论文本”:“c2”,
“CommentCreateStamp”:“2016-03-23 06:05:59”,
}],
}, {
“注释文本”:“a1”,
“CommentCreateStamp”:“2017-03-23 06:05:45”,
“评论”:[{
“评论文本”:“a2”,
“CommentCreateStamp”:“2016-03-23 06:06:05”,
}, {
“评论文本”:“a3”,
“CommentCreateStamp”:“2016-03-23 06:06:16”,
}, {
“评论文本”:“a4”,
“CommentCreateStamp”:“2016-03-23 06:06:23”,
}],
}],
};
函数排序(js_对象,键到排序依据){
功能sortByKey(a,b){
var x=a[输入到排序依据];
变量y=b[输入到排序依据];
回报率((xy)?-1:0);
};
js_object.sort(sortByKey);
};
功能排序建议(注释){
排序(注释,“CommentCreateStamp”);
对于(var i=0;iconsole.log(people.Comments)
您可以创建一个平面数组,用于保存引用并按
CommentCreateStamp
对其进行排序

函数getFlat(o){ 这个。推(o); isArray(o.Comments)和&o.Comments.forEach(getFlat.bind(this)); } var object={“CommentText”:“CommentCreateStamp”:“2016-03-22”,“Comments”:[{“CommentText”:“Comment1”,“CommentCreateStamp”:“2016-03-18”,“Comments”:[{“CommentText”:“Comment1-1-1”,“CommentCreateStamp”:“2016-03-21”,“Comments”:“null}]}]}, 平面=[]; 绑定(平面)(对象); 平面排序(函数(a,b){ 返回a.CommentCreateStamp.localeCompare(b.CommentCreateStamp); });
document.write(''+JSON.stringify(flat,0,4)+'')对@changed答案稍加改进——我发现以下方法效果很好:

导出函数sortObj(jsObj、sortKey、flgReverse){
sortKey=sortKey.split(“.”);
设sk='';
Object.key(sortKey).forEach(函数(key,idx){
sk+='[\''+sortKey[key]+'\']';
});
功能sortByKey(a,b){
var x=评估(“a”+sk);
变量y=评估(“b”+sk);
如果(flgReverse)返回((xy)?1:0));
否则返回((xy)?-1:0);
};
返回jsObj.sort(sortByKey);
}; 

那么您想要一个平面列表?如果结构应该保持不变,您只能在同一级别上排序。@georg,不是平面列表。我希望数据按照
CommentCreateStamp
属性进行排序。最新的注释应位于顶部,并且与子层次结构相同。我更新了JSON对象。请现在检查。@NinaScholz,没有办法对层次结构进行排序?请尝试此链接,希望对您有所帮助。谢谢你的回答,但我希望排序对象不是相同格式的平面数组。