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}
{id: 3, parentId: null, position: 1}at index 3?id: 1, then all "children" ofid: 1(by position), next parent (id: 3), children of that parent, ...