-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,257
  • 5
  • 30
  • 51
copenndthagen
  • 46,750
  • 95
  • 274
  • 417

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,257
  • 5
  • 30
  • 51