I have an array in the following format. I have sorted the array in alphabetical order.But every element in the array, a sub array exist. I would like to sort them in alphabetical order too.
var myObj = [
{
"name":"John",
"items": [
{ "id":1, "car":"maruti" },
{ "id":2, "car":"wolks" },
{ "id":3, "car":"bmw" }
]
},
{
"name":"Peter",
"items": [
{ "id":4, "car":"alto" },
{ "id":5, "car":"swift" },
]
}];
This is the code I have used to sort the main array. Here I would like to sort the 'items' array in alphabetical order.
myObj.sort(function(a, b) {
var nameA=a.name.toLowerCase(), nameB=b.name.toLowerCase();
if (nameA < nameB) {
return -1
}
if (nameA > nameB){
return 1
}
return 0
});