I have data. There are other data of the same type that have children in this data. I want to sort them all according to their 'id' fields, but I couldn't set up the algorithm scheme for it.
What I've tried:
const sortedData: [] = [];
function sortAscending(datas: Group[]) {
sortedData.push(datas.sort((group1, group2) => group1.id - group2.id ));
return sortedData;
}
With this code, I can only sort without children.
I tried to model the picture I wanted to describe:

Note: The returned data will be mapped and used later.
Sample array:
0:
children: [{…}]
id: 1
name: "name1"
[[Prototype]]: Object
1:
children: []
id: 7
name: "name2"
[[Prototype]]: Object
The 'children' field also has 'children', 'id' and 'name' fields just like themselves. What I want is to sort both the data itself and the data in the children field in it according to their 'id' fields.