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 Stack Overflow but not working as per requirement.

7
  • so you want a flat list? Commented Mar 22, 2016 at 17:03
  • you can only sort on the same level, if the structure should remain. Commented Mar 22, 2016 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. Commented Mar 23, 2016 at 1:21
  • @NinaScholz, there is no way to sort hierarichal structure ? Commented Mar 23, 2016 at 4:58
  • Try this link hope its help you. stackoverflow.com/questions/13758467/… Commented Mar 23, 2016 at 8:13

3 Answers 3

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);

Sign up to request clarification or add additional context in comments.

Comments

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>');

1 Comment

Thanks for the answer but i want sorted object not an flat array in same format.
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);
  }; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.