Sort one array based on another array when second array is the key in first array. Lets say I have a main array like below:
let mainArray = [{ "id": 24443, "name": "Apple" }, { "id": 20172, "name": "Banana" }, { "id": 187016, "name": "Cherry" }, { "id": 217632, "name": "Guava" }, { "id": 5221, "name": "Peach" }, { "id": 97568, "name": "Strawberry" }]
And I have id array like below:
let idArray = [ "24443", "20172", "5221", "187016", "217632", "97568" ]
Notice that second array contains values which are "id" property in the first array.
What are the best ways to sort the main array based on the order of occurrence of "key" in the second array?