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

143
Visualizações
How to sort a nested query result

I have the following output:

    {
      "data": {
        "title": "Movie 4",
        "data": [
          {
            "userId": 2,
            "profile": { "profileImage": "image" },
            "round": 1,
          },
         {
            "userId": 4,
            "profile": { "profileImage": "image" },
            "round":6,
          },
          {
            "userId": 10,
            "profile": { "profileImage": "image" },
            "round": 4,
          },
        ]
      }
    }

The output im expecting is the following:

{
  "data": {
    "title": "Movie 4",
    "data": [
      {
        "userId": 2,
        "profile": { "profileImage": "image" },
        "round": 1,
      },
      {
        "userId": 10,
        "profile": { "profileImage": "image" },
        "round": 4,
      },
     {
        "userId": 4,
        "profile": { "profileImage": "image" },
        "round":6,
      },
    ]
  }
}

I want to sort my output by my nested value round.

This is where im collecting all the data, but i dont know how to sort it:

let combinedResult = { 
  title: combinations["data"]["title"],
  data: galleryData.map((item, i) => {
     let combination = combinations["data"]["eventdata"].find(c => c.partner === item.userId);
    return { ...item, round: combination.round, roundStart: combination.roundStart, roundEnd: combination.roundEnd} 
  })
};

I already tried to sort before the return with this but it didnt work:

const sorted = Object.entries(resultAfter)
.sort(([keyA], [keyB]) => keyA.localeCompare(keyB));
almost 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can use sortBy from lodash.

import { sortBy } from "lodash";

const sortedData = sortBy(movie.data.data, "round");

Try it on code sandbox.

almost 4 years ago · Santiago Trujillo Relatório

0

Another option is to use sort() method

let json = {
      "data": {
        "title": "Movie 4",
        "data": [
          {
            "userId": 2,
            "profile": { "profileImage": "image" },
            "round": 1,
          },
         {
            "userId": 4,
            "profile": { "profileImage": "image" },
            "round":6,
          },
          {
            "userId": 10,
            "profile": { "profileImage": "image" },
            "round": 4,
          },
        ]
      }
    }
    
json.data.data.sort((a,b) => a.round - b.round)
console.log(json)

almost 4 years ago · Santiago Trujillo Relatório

0

You can simply sort the array inside the object data. In this way you are editing the same array inside the object data, sort() is a destructive method that doesn't create a copy but modify the original array

let yourData = {
      "data": {
        "title": "Movie 4",
        "data": [
          {
            "userId": 2,
            "profile": { "profileImage": "image" },
            "round": 1,
          },
         {
            "userId": 4,
            "profile": { "profileImage": "image" },
            "round":6,
          },
          {
            "userId": 10,
            "profile": { "profileImage": "image" },
            "round": 4,
          },
        ]
      }
    }
    
    let arrayData = yourData.data.data;
    let result = arrayData.sort(compareFunction);
    console.log(yourData);
    
    function compareFunction(keyA,keyB){
      return keyA.round-keyB.round;
    }

almost 4 years ago · Santiago Trujillo 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