-1

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?

3
  • why do you get this result? Commented Jan 18, 2022 at 16:48
  • Does How to sort array by first item in subarray give you an idea? Commented Jan 18, 2022 at 16:51
  • NinaScholz I don't get this result, this is the result that I would like to get. @AndrewMorton The problem seems specifically tied to the fact that I have an array of objects. I have seen similar posts but I am having a hard time making the leap from multidimensional array to nested array of objects. Commented Jan 18, 2022 at 17:57

1 Answer 1

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"
        }
      ]
    }
  ];
}

Sign up to request clarification or add additional context in comments.

1 Comment

This cannot be made arbitrary so that it is not hard-coded to sort C.A?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.