Answers for "sort object inside object javascript"

26

sort array of object js

const books = [
  {id: 1, name: 'The Lord of the Rings'},
  {id: 2, name: 'A Tale of Two Cities'},
  {id: 3, name: 'Don Quixote'},
  {id: 4, name: 'The Hobbit'}
]

books.sort((a,b) => (a.name > b.name) ? 1 : ((b.name > a.name) ? -1 : 0));
Posted by: Guest on April-08-2021
1

sort array of objects javascript

const books = [
  {id: 1, name: 'The Lord of the Rings'},
  {id: 2, name: 'A Tale of Two Cities'},
  {id: 3, name: 'Don Quixote'},
  {id: 4, name: 'The Hobbit'}
]

books.sort((a, b) => a.name.localeCompare(b.name))
Posted by: Guest on September-16-2021

Code answers related to "sort object inside object javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language