Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
Sorting JSON by Value in jQuery/Javascript

I have a JSON file formatted like below:

{
    "dislike": 0,
    "like": 15,
    "laugh": 4
}

How can I sort the JSON in descending order, so that it looks like this:

{
    "like": 15,
    "laugh": 4,
    "dislike": 0
}

Any help would be much appreciated. Thank you!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I'm using object.entries() to transform the object in an array of entries [["dislike", 0], ["like", 15], ["laugh", 4]] to use the sort method, so I can order by the position [1] of each element in the array that is the entry value.

At the end transform the entries in an object again using Object.fromEntries()

json = {
    "dislike": 0,
    "like": 15,
    "laugh": 4
}

const entriesResult = Object.entries(json).sort((v1, v2) => v2[1] - v1[1])

const resultObject = Object.fromEntries(entriesResult)

console.log(resultObject)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!