I have an array of objects in javascript, each of which in turn has an array:
{
category: [
{ name: "Cat1", elements : [
{ name: name, id: id } ]
},
{ name: "Cat2", elements : [
{ name: name, id: id },
{ name: name, id: id },
{ name: name, id: id } ]
},
{ name: "Cat3", elements : [
{ name: name, id: id },
{ name: name, id: id } ]
}
]
}
I would like to sort the array "category" based on the number of objects within the nested array "elements".
For example, after sorting, the above object might look like this (descending):
{
category: [
{ name: "Cat2", elements : [
{ name: name, id: id },
{ name: name, id: id },
{ name: name, id: id } ]
},
{ name: "Cat3", elements : [
{ name: name, id: id },
{ name: name, id: id } ]
},
{ name: "Cat1", elements : [
{ name: name, id: id } ]
}
]
}
I am wondering if it is possible to accomplish this using javascript's sort() method. Any suggestions?
Thanks in advance!