Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

reverse sort array of objects

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'}
]
// the 1 and -1 can be switched for standard sort
books.sort((a,b) => (a.id > b.id) ? -1 : ((b.id > a.id) ? 1 : 0));
// Code idea from fancyfinch
Source by www.codegrepper.com #
 
PREVIOUS NEXT
Tagged: #reverse #sort #array #objects
ADD COMMENT
Topic
Name
5+2 =