My first array looks like this:
[
{entityId: 1, name: 'Total sales'},
{entityId: 2, name: 'Total customers'},
{entityId: 3, name: 'Reach'}
]
I have a second array which stores entityIds, and I need to sort the first array using the values of the second array. So, if my second array looks like this:
[1, 3, 2]
then my first array should be sorted like this:
[
{entityId: 1, name: 'Total sales'},
{entityId: 3, name: 'Reach'},
{entityId: 2, name: 'Total customers'}
]
I know I can sort an array based on object properties by using the sort method with my own callback:
array.sort(function(a, b) {
// ... sorting logic ...
});
However I'm struggling to wrap my head around how the sorting logic will follow the sorting rules imposed by the second array, rather than just comparing two neighbouring elements.