Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

95
Views
Sort array of objects by two properties JS

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;
  })
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!