-5

How do I sort the following by 'name' property ?

staticdata.items = [
  {id: '0', 'name': 'ABC'},
  {id: '0', 'name': 'XYZ'},
  {id: '0', 'name': 'DEF'}
]

So in the end, staticdata.items should look like

staticdata.items = [
  {id: '0', 'name': 'ABC'},
  {id: '0', 'name': 'DEF'},
  {id: '0', 'name': 'XYZ'}
]
Pugazh
  • 9,453
  • 5
  • 33
  • 54
copenndthagen
  • 49,230
  • 102
  • 290
  • 442

1 Answers1

0

Here is a simple way.

var items = [{
  id: '0',
  'name': 'ABC'
}, {
  id: '0',
  'name': 'XYZ'
}, {
  id: '0',
  'name': 'DEF'
}]

var sortedItems = items.sort(function(pv, cv) {
  return pv.name > cv.name;
})

console.log(sortedItems);
Pugazh
  • 9,453
  • 5
  • 33
  • 54