sort item with parent an child in typescript and angular

Viewed 618

i have this list :

0: {id: 7, name: "333", code: "333", type: 3, hasParent: true, parentId: 4}

1: {id: 6, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: false, parentId: null}

2: {id: 5, name: "111", code: "111", type: 3, hasParent: true, parentId: 4}

3: {id: 4, name: "22", code: "22", type: 1, hasParent: false, parentId: null}

4: {id: 3, name: "yyy", code: "yyyy", type: 2, hasParent: false, parentId: null}

5: {id: 2, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: true, parentId: 1}

6: {id: 1, name: "cbcvb", code: "cvbcvcbv", type: 2, hasParent: false, parentId: null}

and i need sort this list by parent and child .

If the parent value of the item is equal to the value of the id of another item, the item that has the value of the parentId should be placed under the item whose parent value is equal to id value.

like this list :

4: {id: 3, name: "yyy", code: "yyyy", type: 2, hasParent: false, parentId: 6}

1: {id: 6, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: false, parentId: null}

0: {id: 7, name: "333", code: "333", type: 3, hasParent: true, parentId: 4}

2: {id: 5, name: "111", code: "111", type: 3, hasParent: true, parentId: 4}

3: {id: 4, name: "22", code: "22", type: 1, hasParent: false, parentId: null}

5: {id: 2, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: true, parentId: 1}

6: {id: 1, name: "cbcvb", code: "cvbcvcbv", type: 2, hasParent: false, parentId: null}

i write this code but it not worked and not sorted the list of items :

    var Data = [{ id: 7, name: "333", code: "333", type: 3, hasParent: true, parentId: 4 },
{ id: 6, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: false, parentId: null },
{ id: 5, name: "111", code: "111", type: 3, hasParent: true, parentId: 4 },
{ id: 4, name: "22", code: "22", type: 1, hasParent: false, parentId: null },
{ id: 3, name: "yyy", code: "yyyy", type: 2, hasParent: false, parentId: null },
{ id: 2, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: true, parentId: 1 },
{ id: 1, name: "cbcvb", code: "cvbcvcbv", type: 2, hasParent: false, parentId: null }];


var result = [];
Data.forEach((values) => {
    if (result.indexOf(values) === -1) {
        result.push(values);
    }
    if (values.parentId !== null) {
        var d_ = Data.filter(srch => {
            return values.parentId === srch.id;
        });
        if (result.indexOf(d_[0]) === -1) {
            result.push(d_[0]);
        }
    }
});
console.log(result);

whts the problem ? how can i solve this problem ????

1 Answers

If it's not nested the elements, I think you can do some like (but not give you the same result -really I think you has an error, parentId of 3 are not 6?-)

this.sorted=[];
//get all the elements that has parent
const childs=this.data.filter(x=>x.parentId).sort((a,b)=>a.parentId-b.parentId)

//for each
childs.forEach((x,i)=>{
  this.sorted.push(x)     //<--add to sorted
                          //if is the last or the next has diferent parent
  if (i==childs.length-1 || childs[i+1].parentId!=x.parentId)
     this.sorted.push(this.data.find(p=>p.id==x.parentId)) //<--add the parent

})
//finally include the elements that is not in the list
this.data.forEach(x=>{
  if (!this.sorted.find(s=>s.id==x.id))
    this.sorted.push(x)
})
Related