Answers for "sort array of objects and store in new array 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
0

order array of objects by id javascript

items.sort(function(a, b) { 
  return a.id - b.id  ||  a.name.localeCompare(b.name);
});
Posted by: Guest on September-07-2021

Code answers related to "sort array of objects and store in new array javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language