Here is a list that needs to be sorted by parent and child relationships:
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}
In order to achieve this sorting, the items with parent values equal to another item's id should be placed below that item.
The desired sorted list should look like this:
...
I attempted to write a code snippet for this sorting task, but unfortunately, it did not work as intended. The resulting list was not sorted according to the logic I described above.
Can you help me identify what went wrong in my code and suggest a solution to fix this issue?