I have a confusing problem right now. The situation is this:
let example = [
{
name: "First",
id: 1,
rank: 1,
childs: [5,3] // [3,5] -> wrong, because the element which have the id: 3
//(First's second child), have the rank 2, but with id: 5 (First's first child) have the rank 1.
},
{
name: "First's first child",
id: 5,
rank: 1,
childs: [4,2]
},
{
name: "First's second child",
id: 3,
rank: 2,
childs: [6]
},
{
name: "Second's first child",
id: 4,
rank: 1,
childs: []
},
{
name: "Second's second child",
id: 2,
rank: 2,
childs: []
},
{
name: "Third's first child",
id: 6,
rank: 1,
childs: []
}
]
The childs property is an array which shows the element's childs ids (the names of the items shows the relationship between the elements). The childs array's elements order must be equal the elements ranks which has the same id the childs has.
The problem here is, that the childs array's sorting is not always correct, which generates a lot of problems for me. What i need to do is get the element's childs (the childs items of the array means the item's id in example), search each of them in the example array, then sort the childs array in order of it's ranks in example. So i want to sort example[i].childs, depending on the same id'd item's rank in example. How is this possible?
childsare sorted. But it must sorted depending on it's rank.childsarray should maintain the same order as is found in theexamplearray? Or they should be sorted ascending by rank?