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

130
Vistas
Sorting nested array of objects

I have an array of objects that has a nested array of objects in it. So the array looks something like:

const list = [
  {
    A: "a1",
    B: "b1",
    C: [
      {
        A: "a22",
        B: "b12"
      },
      {
        A: "a11",
        B: "b11"
      },
      {
        A: "a10",
        B: "b10"
      }
    ]
  },
  {
    A: "a2",
    B: "b2",
    C: [
      {
        A: "a10",
        B: "b10"
      },
      {
        A: "a01",
        B: "b01"
      }
    ]
  },
  {
    A: "a0",
    B: "b0",
    C: [
      {
        A: "a22",
        B: "b22"
      },
      {
        A: "a21",
        B: "b21"
      },
      {
        A: "a20",
        B: "b20"
      }
    ]
  }
];

As can be seen I have an array of objects and each object as one or more fields that is also an array of objects. I can sort the array of objects based on one of the keys and it works just fine. What I want to do is sort by one of the keys in the nested array. For example sorting on C.A would yield something like (expected):

[
  {
    A: "a0",
    B: "b0",
    C: [
      {
        A: "a22",
        B: "b22"
      },
      {
        A: "a21",
        B: "b21"
      },
      {
        A: "a20",
        B: "b20"
      }
    ]
  },
  {
    A: "a1",
    B: "b1",
    C: [
      {
        A: "a12",
        B: "b12"
      },
      {
        A: "a11",
        B: "b11"
      },
      {
        A: "a10",
        B: "b10"
      }
    ]
  },
  {
    A: "a2",
    B: "b2",
    C: [
      {
        A: "a10",
        B: "b10"
      },
      {
        A: "a01",
        B: "b01"
      }
  }
];

Ideas?

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

0

The way to get one's head clear on such things is to factor out the sort functions to explicitly state the objective, like this (I think I understand the objective)...

// sort a and b by the smallest value of A in their C arrays
const myCompare = (a, b) => {
  return a.minAinC.localeCompare(b.minAinC);
};

// get the lexically smallest value of A in an object's C array
const minAinC = obj => {
  const minC = obj.C.reduce((acc, o) => acc.A.localeCompare(o.A) > 0 ? o : acc, obj.C[0])
  return minC.A;
};

// preprocess the outer array and cache a minAinC value on each, making the next sort efficient (optional)

const data = getData()
const readyToSort = data.map(o => ({ ...o, minAinC: minAinC(o) }));

const sorted = readyToSort.sort(myCompare)
console.log(sorted)

function getData() {
  return [{
      A: "a1",
      B: "b1",
      C: [{
          A: "a22",
          B: "b12"
        },
        {
          A: "a11",
          B: "b11"
        },
        {
          A: "a10",
          B: "b10"
        }
      ]
    },
    {
      A: "a2",
      B: "b2",
      C: [{
          A: "a10",
          B: "b10"
        },
        {
          A: "a01",
          B: "b01"
        }
      ]
    },
    {
      A: "a0",
      B: "b0",
      C: [{
          A: "a22",
          B: "b22"
        },
        {
          A: "a21",
          B: "b21"
        },
        {
          A: "a20",
          B: "b20"
        }
      ]
    }
  ];
}

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