I am facing a challenge with sorting an array of objects in two specific ways:
- First, I need to sort the objects by alphabetical order using the country name.
- Then, inside each country, I have to arrange the scores in ascending order.
Below you can find the original array and the code snippet I attempted to use for this task.
//Array Structure
let allCountryScores=[
{
name: "Ethiopia",
scores: [ {n: "Sub", s: 9990}, {n: "lucy", s: "4134234"}, {n: "DWH", s: 9999}, {n: "Hanif", s: 999999999} ]
},
]
//Code Attempt
//Sorting countries alphabetically
let orderedCountries = allCountryScores.sort((a, b) => a.name.localeCompare(b.name));
//Sorting scores numerically within each country
let orderedScores = orderedCountries.forEach(country => { country.scores.sort((a, b) => a.s > b.s) });
console.log(orderedScores);
When I check my console, it returns 'undefined'. Can anyone help me identify what might be causing this issue in my code?