I’m trying to find a way to rearrange an array based on the order specified in another array. For instance, I have the following data structure:
elements = [
['Alice', 'x'],
['Charlie', 'y'],
['Eve', 'y'],
['Mallory', 'z'],
['Oscar', 'w'],
['Trent', 'y']
]
And I want to sort it according to this reference:
orderPattern = ['y', 'w', 'y', 'y', 'x', 'z']
I don’t have unique identifiers to help with matching, and my goal is to reorder elements
closely following orderPattern
. The desired output should look like this:
elements = [
['Charlie', 'y'],
['Trent', 'y'],
['Eve', 'y'],
['Oscar', 'w'],
['Alice', 'x'],
['Mallory', 'z']
]
What would be the best way to achieve this?