javascriptphpalgorithmcommentsreply

sorting comments data algorithm with php or javascript


const comments = [
    { 'id': 1, 'origin_id': 0 },
    { 'id': 2, 'origin_id': 0 },
    { 'id': 3, 'origin_id': 2 },
    { 'id': 4, 'origin_id': 2 },
    { 'id': 5, 'origin_id': 3 },
    { 'id': 6, 'origin_id': 3 },
    { 'id': 7, 'origin_id': 0 },
    { 'id': 8, 'origin_id': 0 },
    { 'id': 9, 'origin_id': 0 },
    { 'id': 10, 'origin_id': 0 },
];

// or? data structure somthing like this? i dont konw how to use such order and depth.

const comments = [
    { 'id': 1, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
    { 'id': 2, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
    { 'id': 3, 'origin_id': 2, 'reply_order': 0, 'reply_depth': 0 },
    { 'id': 4, 'origin_id': 2, 'reply_order': 0, 'reply_depth': 0 },
    { 'id': 5, 'origin_id': 3, 'reply_order': 0, 'reply_depth': 0 },
    { 'id': 6, 'origin_id': 3, 'reply_order': 0, 'reply_depth': 0 },
    { 'id': 7, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
    { 'id': 8, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
    { 'id': 9, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
    { 'id': 10, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
];

i want some algorithm something like comments in comment. is that even possible with only that amount of data?

if someone knows any better way to implements that goals, please, tell me.

i want to concept of this. any code lang would be OK with me. but i prefer PHP and JS.

const comments = [
    { 'id': 1, 'origin_id': 0 },
    { 'id': 2, 'origin_id': 0 },
    { 'id': 3, 'origin_id': 2 },
    { 'id': 4, 'origin_id': 2 },
    { 'id': 5, 'origin_id': 3 },
    { 'id': 6, 'origin_id': 3 },
];

// result i expected;
const result_comments = [
    { 'id': 1, 'origin_id': 0 },
    {
        'id': 2, 'origin_id': 0,
        'children': [{
            'id': 3, 'origin_id': 2, 'children': [{ 'id': 5, 'origin_id': 3 },
            { 'id': 6, 'origin_id': 3 },]
        }, { 'id': 4, 'origin_id': 2 }]
    },
];

Solution

  • The nested object here is basically like a tree and comments become tree nodes. first, create a dictionary with children's property and then assign the children from it.

    comments id are created in ascending order, so you don't need to re-sort them after creating.

    // assuming that id are sorted, else you have to sort comments by id.
    const comments = [
        { 'id': 1, 'origin_id': 0 },
        { 'id': 2, 'origin_id': 0 },
        { 'id': 3, 'origin_id': 2 },
        { 'id': 4, 'origin_id': 2 },
        { 'id': 5, 'origin_id': 3 },
        { 'id': 6, 'origin_id': 3 },
    ];
    
    // creating dictinary where key is id and value is object with added children property
    let dic = comments.reduce((acc, obj) => (acc[obj.id] = {...obj, children : [] }, acc), {});
    
    // initial set 
    let treeRoot = {'id': 0, 'origin_id': 0 , children : []};
    
    // adding in dictionary so i can add origin_id 0 to tree root
    dic[treeRoot.id] = treeRoot;
    
    // populating the childrens
    comments.forEach(obj => dic[obj.origin_id].children.push(dic[obj.id]));
    
    // if you want the array that inside treeRoot.children
    console.log(treeRoot);