1

Summary : there is a multilevel parent-child comment system.i.e.

Example :

comment 1
    comment 1-1
        comment 1-1-1
    comment 1-2
        comment 1-2-1
             comment 1-2-2

Here, comment 1 is parent and comment 1-1 & comment 1-2 both are child of comment 1 and so on..

Requirement :

we want to achieve the sorting of whole object based on property value.i.e. timestamp(latest comment or reply will come on the top).

JSON Object :

{
  "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
      }]
    }]
  }]
}

i tried so far :

JSfiddle : https://jsfiddle.net/asceeevb/

I seen lot of questions on stackoverflow but not working as per requirement.

Any immediate help will be highly appreciable. Thanks

Ravimallya
  • 6,214
  • 2
  • 39
  • 74
Rohit Jindal
  • 11,704
  • 11
  • 56
  • 92
  • so you want a flat list? – georg Mar 22 '16 at 17:03
  • you can only sort on the same level, if the structure should remain. – Nina Scholz Mar 22 '16 at 17:11
  • @georg, not a flat list. i want the data in sorted way according to the `CommentCreateStamp` property.the latest comments should come on the top and same with child hierarchy also. I updated the JSON object. please check now. – Rohit Jindal Mar 23 '16 at 01:21
  • @NinaScholz, there is no way to sort hierarichal structure ? – Rohit Jindal Mar 23 '16 at 04:58
  • Try this link hope its help you. http://stackoverflow.com/questions/13758467/how-do-i-sort-a-json-object-by-a-nested-value – Bhupesh Mar 23 '16 at 08:13
  • Try this link i hope its work for you. http://stackoverflow.com/questions/13758467/how-do-i-sort-a-json-object-by-a-nested-value – Bhupesh Mar 23 '16 at 08:14
  • Try this link http://stackoverflow.com/questions/13758467/how-do-i-sort-a-json-object-by-a-nested-value – Bhupesh Mar 23 '16 at 08:15

3 Answers3

2

I am not entirely sure what you are asking, but there a couple of things that seem wrong with your code:

  • you redefined the comments property of the object, perhaps you wanted an array?
  • your sorting function is supposed to return a value based on comparison of elements, not output it. See this SO question for date comparison.

Update: You were close, what you were missing were calls to sorting for the nested comments. Here is a snippet that calls sorting recursively:

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": "2017-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(js_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));
  };

  js_object.sort(sortByKey);

};

function sortComments(comments) {
  
  sorting(comments, 'CommentCreateStamp');
  for (var i = 0; i < comments.length; i++)
    if (comments[i].hasOwnProperty('Comments'))
      sortComments(comments[i].Comments);

};

sortComments(people.Comments);
console.log(people.Comments);
Community
  • 1
  • 1
pishpish
  • 2,359
  • 11
  • 21
1

You could make a flat array which keeps the reference and sort it by CommentCreateStamp.

function getFlat(o) {
    this.push(o);
    Array.isArray(o.Comments) && o.Comments.forEach(getFlat.bind(this));
}

var object = { "CommentText": "", "CommentCreateStamp": "2016-03-22", "Comments": [{ "CommentText": "comment 1", "CommentCreateStamp": "2016-03-18", "Comments": [{ "CommentText": "comment 1-1", "CommentCreateStamp": "2016-03-15", "Comments": [{ "CommentText": "comment 1-1-1", "CommentCreateStamp": "2016-03-21", "Comments": null }] }] }] },
    flat = [];

getFlat.bind(flat)(object);
flat.sort(function (a, b) {
    return a.CommentCreateStamp.localeCompare(b.CommentCreateStamp);
});
document.write('<pre>' + JSON.stringify(flat, 0, 4) + '</pre>');
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324
0

Refining the @changed answer slightly -- I find that the following works pretty well:

  export function sortObj(jsObj, sortKey,flgReverse) {
    sortKey = sortKey.split(".");
    let sk = '';
    Object.keys(sortKey).forEach(function(key,idx){
    sk += '[\'' + sortKey[key] + '\']';
    });
    function sortByKey(a, b) {
      var x = eval("a" + sk);
      var y = eval("b" + sk);
      if(flgReverse) return  ((x < y) ? -1 : ((x > y) ? 1 : 0));
      else return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    };
    return jsObj.sort(sortByKey);
  }; 
jomofrodo
  • 905
  • 9
  • 17