JavaScript: Sort array of objects based on a particular property of the objects in a specific order

I have an array of objects containing a particular structure. Then, I have an array (given by the user), which contains values in the order which the elements should be sorted. For simplicity, I'll give the following example:

const arrayOfObjects = [
{objectId: 1, color: 'Orange'},
{objectId: 2, color: 'Blue'},
{objectId: 3, color: 'White'},...

And the array given by the user for sorting:

['White', 'Orange', 'Blue']

Now my question is, how can I sort the first array, so that all objects with color white come first, then all the elements with orange color and then blue?

Note: The array will be given by the user, so cannot sort alphabetically.

Read more here:

Content Attribution

This content was originally published by Vaibhav Joshi at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: