JSFiddle - React, Tailwind, and code Playground
JavaScript
var people = {
"CommentText": "",
"CommentCreateStamp": "",
"Comments": [
{
"CommentText": "c1",
"CommentCreateStamp": "2016-03-23 06:05:36",
"Comments": [
{
"CommentText": "c2",
"CommentCreateStamp": "2016-03-23 06:05:59",
}
],
},
{
"CommentText": "a1",
"CommentCreateStamp": "2016-03-23 06:05:45",
"Comments": [
{
"CommentText": "a2",
"CommentCreateStamp": "2016-03-23 06:06:05",
},
{
"CommentText": "a3",
"CommentCreateStamp": "2016-03-23 06:06:16",
},
{
"CommentText": "a4",
"CommentCreateStamp": "2016-03-23 06:06:23",
}
],
}
],
}
function sorting(json_object, key_to_sort_by) {
function sortByKey(a, b) {
var x = a[key_to_sort_by];
var y = b[key_to_sort_by];
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
}
json_object.sort(sortByKey);
}
sorting(people.Comments, 'CommentCreateStamp');
console.log(people)