I have an array of objects, with each element containing another nested object like this:
data = [
{
name: "A",
type: "AA",
children: [ { id: 1, name: "Child-A", admin: ["Y"] }],
other: "NA"
},
{
name: "B",
type: "BB",
children: [ { id: 2, name: "Child-B" }],
other: "NA"
},
{
name: "C",
type: "CC",
children: [ { id: 3, name: "Child-C" }],
other: "NA"
}
]
I need to sort the entire collection by the children.id
, following a specific order defined in another array:
orderArray = [3, 1, 2]
Therefore, the desired output would be:
data =[
{
name: "C",
type: "CC",
children: [ { id: 3, name: "Child-C" }],
other: "NA"
},
{
name: "A",
type: "AA",
children: [ { id: 1, name: "Child-A", admin: ["Y"] }],
other: "NA"
},
{
name: "B",
type: "BB",
children: [ { id: 2, name: "Child-B" }],
other: "NA"
}
]