In Javascript, I have an array of values such as:
["Toy","Car","PC","Water","Apple"]
I need to order the values a specific order in this priority:
PC, Car, Toy, Apple, Water
Note, this cannot be sorted alphabetic, but in a specific order of values provided.
How would I best accomplish this? After it is ordered, I will be iterating it in a FOR loop, and needs to stay in that order. Also, keeping it in an array would be best.
Also, if we can add any values not specified in the "priority order list" to be added on at the end of the resulting array, alphabetically, that would be ideal.
Sometimes the passed array will not contain all the values specified in the priority order list, hence why I cannot use the priority order list itself.
Example 2
Incoming array: ["Green","Blue","Yellow","Red"]
Provided preferred order of values: Yellow, Black, Silver, Blue, Red, Green, Grey
Expected output: ["Yellow","Blue","Red","Green"]
Example 3
In this case, the incoming has more values than the order list.
Incoming array: ["Green","Blue","Yellow","Red","Silver","Black]
Provided preferred order of values: Yellow, Black, Silver
Expected output: ["Yellow","Black","Silver","Green","Blue","Red"]
The additional values should be appended to the outputted list, preferably in alphabetical order for the extra values.