Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

sort a dictionary by value in 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'}
]

compareObjects(object1, object2, key) {
  const obj1 = object1[key].toUpperCase()
  const obj2 = object2[key].toUpperCase()

  if (obj1 < obj2) {
    return -1
  }
  if (obj1 > obj2) {
    return 1
  }
  return 0
}

books.sort((book1, book2) => {
  return compareObjects(book1, book2, 'name')
})
Comment

PREVIOUS NEXT
Code Example
Javascript :: isset js 
Javascript :: for each python json 
Javascript :: vite install in vue 
Javascript :: require("history").createBrowserHistory` instead of `require("history/createBrowserHistory")` 
Javascript :: express ejs layout use different layout 
Javascript :: how to change background image for a webpage 
Javascript :: javascript remove event listener 
Javascript :: statusbar.sethidden(true) in react native 
Javascript :: js import export es5 
Javascript :: js docstring example 
Javascript :: javascript date to string format 
Javascript :: how to import jquery in js file 
Javascript :: javascript insert last character of string 
Javascript :: javascript dedupe array 
Javascript :: react-select dropdown open inside modal 
Javascript :: FAILURE: Build failed with an exception react native android 
Javascript :: Ts get first string char 
Javascript :: exceljs read file 
Javascript :: vue.js function to always uppercase when the client input lowercase 
Javascript :: jquery validator no space 
Javascript :: why does my page reloads on form submission 
Javascript :: javascript foreach array of object get value by key 
Javascript :: javascript object destructuring rename 
Javascript :: javascript get distinct values from array 
Javascript :: javascript every other element in array 
Javascript :: vue fetch api 
Javascript :: remove first and last character javascript 
Javascript :: react router next page top 
Javascript :: node get root directory 
Javascript :: js transitions 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =