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

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

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 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!