sort a dictionary by value in javascript

Solutions on MaxInterview for sort a dictionary by value in javascript by the best coders in the world

showing results for - "sort a dictionary by value in javascript"
Andrés
08 Mar 2018
1const books = [
2  {id: 1, name: 'The Lord of the Rings'},
3  {id: 2, name: 'A Tale of Two Cities'},
4  {id: 3, name: 'Don Quixote'},
5  {id: 4, name: 'The Hobbit'}
6]
7
8compareObjects(object1, object2, key) {
9  const obj1 = object1[key].toUpperCase()
10  const obj2 = object2[key].toUpperCase()
11
12  if (obj1 < obj2) {
13    return -1
14  }
15  if (obj1 > obj2) {
16    return 1
17  }
18  return 0
19}
20
21books.sort((book1, book2) => {
22  return compareObjects(book1, book2, 'name')
23})