I have some json data which looks something like this:
const data = {
"stores": [
{
"name": "s1",
"id": "6fbyYnnqUwAEqMmci0cowU",
"customers": [
{
"id": "4IhkvkCG9WWOykOG0SESWy",
"name": "customer2",
},
{
"id": "4IhkfikCG9WWOykOG0SESWy",
"name": "customer1",
},
{
"id": "9IfkvkCG9WWOykOG0SESWy",
"name": "customer100",
},
]
},
{
"name": "s2",
"id": "2D1fTgjAJ20ImiAqsWAEEI",
"customers": [
{
"id": "3dfkvkCG9WWOykOG0SESWy",
"name": "customer9",
},
]
},
{
"name": "s3",
"id": "6rxmPFzIHKQ0EOAGGkAwKg",
"customers": [
{
"id": "7IfkvkCG9WWOykOG0SESWy",
"name": "customer7",
},
]
}
]
}
I need to sort the data as per the customers name. What I have done so far:
const sortedData = data.stores.sort(function(c1, c2) {
return c1.customers[0].name < c1.customers[0].name;
}).map(storeInfo => (
// Need to do something else with sorted data
)
My approach does not seem to work because I think it does not go to into nested level. Any help is appreciated.
Expected Outcome: Sort customers list(alphabetically) within each store first and then among the stores.