Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!