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

311
Vistas
What should I do to sort an object by name ascending and descending

I have tried to find a solution and could find something that is close but not there yet. Javascript sort an object by keys based on another Array? I want to sort them by ABC ascending and descending and I don't know where to start.

const Accounts = {
  'thing': {
    password: '123',
  }
}

const sortedKeys = Object.getOwnPropertyNames(Accounts).sort();
//This returns the names but not the passwords
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

const sortedKeys = Object.getOwnPropertyNames(Accounts).sort();

The above sortedKeys will contain an array of sorted keys as you have seen. You can then map the sorted keys to their values and have a sorted object.

The code is a simpler version of what I just explained:

const Accounts = {
  thing: { password: "123 }
}

const sortedAccounts = Object.keys(Accounts)
  .sort()
  .reduce((accumulator, key) => {
    accumulator[key] = obj[key];

    return accumulator;
  }, {});

about 4 years ago · Juan Pablo Isaza Denunciar

0

If I understood your question correctly, then it's essentially a duplicate of this question.

From the linked question's comments: "The ordering of object properties is non-standard in ECMAScript", so you can't do what you want to do directly.

Here's how I'd do it:

const Accounts = {
  'thing': {
    password: '123',
  }
} 

//get array keys and sort them
let sorted_keys = Object.keys(Accounts).sort();

//if you need your values too then use keys to access
sorted_keys.forEach(function(key) {//access each key and map to its value
    let value = Accounts[key]
    //do whatever with value
}
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