How can I approach and align elements in the array?

You can use fromEntries to convert your array item into object and then sort. But you should consider updating the item to object to avoid this unnecessary conversion.

const list = [ [ ['firstName', 'Joe'], ['age', 42], ['gender', 'male'], ], [ ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['gender', 'female'], ], [ ['lastName', 'Kim'], ['age', 40], ['gender', 'female'], ], ];
list.sort((a, b) => Object.fromEntries(a).age - Object.fromEntries(b).age);

console.log(list);


You can do the following by finding the index of the child array where we can find age. From the example we can see that age is at index 0 and the age value is at index 1 of the sub array. You can than compare the age values.

 list = [
  [
    ['firstName', 'Joe'],
    ['age', 42],
    ['gender', 'male'],
  ],
  [
    ['firstName', 'Mary'],
    ['lastName', 'Jenkins'],
    ['age', 36],
    ['gender', 'female'],
  ],
  [
    ['lastName', 'Kim'],
    ['age', 40],
    ['gender', 'female'],
  ],
];

res = list.sort((a, b) => {
  ageIndexA = a.findIndex(item => item[0] ==='age');
  ageIndexB = b.findIndex(item => item[0] === 'age');
  return a[ageIndexA][1] - b[ageIndexB][1];
});

console.log(res);


The following should work:

    let list = [
  [
    ['firstName', 'Joe'],
    ['age', 42],
    ['gender', 'male'],
  ],
  [
    ['firstName', 'Mary'],
    ['lastName', 'Jenkins'],
    ['age', 36],
    ['gender', 'female'],
  ],
  [
    ['lastName', 'Kim'],
    ['age', 40],
    ['gender', 'female'],
  ],
];
const compareFunction = (a,b) => {
  return a.age - b.age;
}
const sortList = (arr) => {
  const objectList = arr.map(e => Object.fromEntries(e));
  return objectList.sort(compareFunction);
}
console.log('sorted:', sortList(list));