I am faced with the challenge of sorting a multidimensional array based on values, but the selection is dependent on the parentID
.
This is my current array:
const result = [
[{value: 123, parentID: 1}, {value: 'string123', parentID: 2}],
[{value: 54764, parentID: 1}, {value: 'string321', parentID: 2}],
[{value: 321, parentID: 1}, {value: 'string565632', parentID: 2}],
]
Here's what I have attempted:
const parentID = 1;
const sortedResult = result.filter((row) => {
const selectedColumn = row.find((column) => column.parentID === parentID));
return _.orderBy(selectedColumn, ['value'], ['asc']);
});
Unfortunately, this approach does not yield the desired outcome. Any suggestions on how to correct it?
The expected result should be:
[
[{value: 123, parentID: 1}, {value: 'string123', parentID: 2}],
[{value: 321, parentID: 1}, {value: 'string565632', parentID: 2}],
[{value: 54764, parentID: 1}, {value: 'string321', parentID: 2}],
]