Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda