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

178
Vistas
How can I sort an object alphabetically by value?

So I have a list of countries, by default they come sort by ISOA2 Code which is some kind of country code.

Say this list:

{
  "AD": "Andorra",
  "AE": "United Arab Emirates",
  "AF": "Afghanistan",
  "AG": "Antigua and Barbuda",
  "AI": "Anguilla",
  "AL": "Albania",
  "AM": "Armenia",
  "AN": "Netherlands Antilles",
  "GB": "United Kingdom"
}

And I have this code:

    function sortObject(obj) {
      return Object
        .keys(obj)
        .sort()
        .reduce((a, v) => {
          a[v] = obj[v];
          return a;
        }, {});
    }

    
    // data is the list of countries
    const result = sortObject(data);

I am trying to sort it with that code but it always returns the same list.

I need the list to be like this:

{
  "AF": "Afghanistan",
  "AL": "Albania",
  "AD": "Andorra",
  "AI": "Anguilla",
  "AG": "Antigua and Barbuda",
  "AM": "Armenia",
  "AN": "Netherlands Antilles",
  "AE": "United Arab Emirates",
  "GB": "United Kingdom"
}

What am I doing wrong?

You can play around here:

https://codesandbox.io/s/js-playground-forked-xk005?file=/src/index.js

You will see the result in the console.

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

0

Take the Object.entries sort them by value, then use Object.fromEntries to turn this array of key-value pairs back into an object:

 Object.fromEntries(
   Object.entries(obj)
    .sort(([keyA, valueA], [keyB, valueB]) => valueA.localeCompare(valueB))
  )

Note that sorting objects is senseless if there are numeric keys or if the object is mutated (as that changes sort order again).

about 4 years ago · Juan Pablo Isaza Denunciar

0

Does this works for you?

function sortObject(obj) {
  return Object.fromEntries(Object
    .entries(obj)
    .sort((a,b)=>a[1].localeCompare(b[1])));
}

const data = {
  "AD": "Andorra",
  "AE": "United Arab Emirates",
  "AF": "Afghanistan",
  "AG": "Antigua and Barbuda",
  "AI": "Anguilla",
  "AL": "Albania",
  "AM": "Armenia",
  "AN": "Netherlands Antilles",
  "GB": "United Kingdom"
}

// data is the list of countries
const result = sortObject(data);

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try this for sorting:

const sortObject = obj => {
  const ret = {}
  Object.values(obj)
      .sort()
      .forEach(val => {
          const key = Object.keys(obj).find(key => obj[key] === val)
          ret[key] = val
      })
  return ret
}
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