I have an array like this:
unorderedArr = ['pear', 'apple', 'banana', 'peach', 'pineapple'];
I would like to order this array based on a other given array like this:
order = ['peach', 'apple', 'pineapple']
The preferred result would be:
orderedArr = ['peach', 'apple', 'pineapple', 'banana', 'pear'];
It doesn't matter on what index the words that are not in the order array are placed. It's not guaranteed that the words in the order array will be in the unordered array
Current Code Solution
I tried using a switch statement like this:
const orderedArr = []
unorderedArr.forEach(word => {
switch (word) {
case 'peach':
orderedArr.push(word);
break;
case 'apple':
orderedArr.push(word);
break;
case 'pineapple':
orderedArr.push(word);
break;
default:
orderedArr.push(word);
break;
}
});
But I found out that a switch statement doesn't check on the first case first.