Home > Blockchain >  Javascript: Hierarchical object sorting
Javascript: Hierarchical object sorting

Time:03-16

I have an array of objects and every objects has the fields id, parentId and position. The position field stores the relative position of that item, so if I make a sort function like this:

object.sort((a, b) => {
  if(a.position < b. position) return -1;
  if(a.position > b. position) return 1;
  return 0;
})

It ends up like this:

{id: 1, parentId: null, position: 0}
{id: 3, parentId: null, position: 1}
{id: 5, parentId: 1, position: 1}
{id: 6, parentId: 3, position: 1}
{id: 4, parentId: null, position: 2}
{id: 2, parentId: 1, position: 2}

How to check both parentId and position to end up like this?

{id: 1, parentId: null, position: 0}
{id: 5, parentId: 1, position: 1}
{id: 2, parentId: 1, position: 2}
{id: 3, parentId: null, position: 1}
{id: 6, parentId: 3, position: 1}
{id: 4, parentId: null, position: 2}

CodePudding user response:

You could collect all nodes by their parentId and get the ordered result starting with null and by the ordered arrays.

const
    getNodes = parent => (parents[parent] || [])
        .sort((a, b) => a.position - b.position)
        .flatMap(o => [o, ...getNodes(o.id)]),
    data = [{ id: 1, parentId: null, position: 0 }, { id: 2, parentId: 1, position: 2 }, { id: 3, parentId: null, position: 1 }, { id: 6, parentId: 3, position: 1 }, { id: 4, parentId: null, position: 2 }, { id: 5, parentId: 1, position: 1 }],
    parents = data.reduce((r, o) => ((r[o.parentId] ??= []).push(o), r), {}),
    result = getNodes(null);

console.log(result);
console.log(parents);
.as-console-wrapper { max-height: 100% !important; top: 0; }

  • Related