0

i have array of country details such as flag name currencies capital timezone population and many more but in this array first country is kuwait that is start from k and i want to sort this array on the bases of country name

const data = 
  [ { name: { common: 'Kuwait', official: 'State of Kuwait'   } } 
  , { name: { common: 'Palau',  official: 'Republic of Palau' } }
  ...

the path of country name

const name = array[0].name.common
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
  • Just you sort method with custom callback, array.sort((a, b) => a.name.common.localeCompare(b.name.common)) – Alopwer Aug 22 '22 at 11:09

1 Answers1

0

you can use sort to do it

const data = [{
    "name": {
      "common": "Zorro",
      "official": "State of Zorro",
    },
  },
  {
    "name": {
      "common": "Kuwait",
      "official": "State of Kuwait",
    },
  },
  {
    "name": {
      "common": "Palau",
      "official": "Republic of Palau",
    },
  }
].sort((a, b) => a.name.common.localeCompare(b.name.common))

console.log(data)
R4ncid
  • 6,944
  • 1
  • 4
  • 18