I have a long array of objects like this. How do I sort it by continents first and alphabetically by countries for each continent?
Sorry, if my wording is wrong. My first question on SO :)
[
{country: 'Afghanistan', continent: 'Asia'},
{country: 'Åland Islands', continent: 'Europe'},
{country: 'Albania', continent: 'Europe'},
{country: 'Algeria', continent: 'Africa'},
{country: 'American Samoa', continent: 'Oceania'},
{country: 'Andorra', continent: 'Europe'},
{country: 'Angola', continent: 'Africa'},
{country: 'Anguilla', continent: 'North America'},
{country: 'Antarctica', continent: 'Antarctica'},
]
I have tried this code and only managed to sort it by countries.
myArray.sort((a, b) => {
const countryA = a.country;
const countryB = b.country;
const continentA = a.continent;
const continentB = b.continent;
return countryA.localeCompare(countryB) || continentA - continentB;
})