Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

94
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda