In my angular project, I have a dataset that needs to be sorted in a specific way.
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}
I am looking to organize this list within an Angular Material table based on parent-child relationships.
This is the criteria:
If the parentId of an item matches the id of another item, the one with the corresponding parentId should appear below the item with the matching id as its parent value.
The items should be sorted as follows:
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'm seeking help on how to properly implement this sorting logic.