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

163
Vistas
how do I sort object by first element in value list

I have javascript object like this,

var list = {
  "you": [100,'x',67],
  "me": [456,'xxx',68],
  "foo": [7856,'yux',69],
  "bar": [2,'xcv',45]
};

I am trying to sort it according to the first element in the value list. like this

var list = {
    "foo": [7856,'yux',69],
    "me": [456,'xxx',68],
    "you": [100,'x',67],
    "bar": [2,'xcv',45],
};

I couldn't find any resources, with similar implementation in java script.

can anyone help?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I totally agree with @Andreas about Does JavaScript guarantee object property order? . But maybe an array output is what you expect.

var list = {
  "you": [100,'x',67],
  "me": [456,'xxx',68],
  "foo": [7856,'yux',69],
  "bar": [2,'xcv',45]
};
var arr = [];
for (const [key, value] of Object.entries(list)) {
  arr.push({k:key, v:value})
}
arr = arr.sort((a, b) => b.v[0] - a.v[0])
console.log(arr);

about 4 years ago · Juan Pablo Isaza Denunciar

0

thanks @YuTing for the help.

working solution

var sortable = [];
for (var i in list) {
    sortable.push([i, list[i]])
}

sortable.sort(function(a, b) {
    return b[1][0] - a[1][0];
});

// console.log(sortable);
var objSorted = {}
sortable.forEach(function(item){
    objSorted[item[0]]=item[1]
})

console.log(objSorted);
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