I have an array of currencies ["GBP", "EUR", "NOK", "DKK", "SKE", "USD", "SEK", "BGN"]. I would like to order it by moving predefined list if the currency is present to the beginning of array. Predefined list is ['EUR', 'USD', 'DKK', 'SKE', 'NOK', 'GBP'].
So in this case it should return ['EUR', 'USD', 'DKK', 'SKE', 'NOK', 'GBP', 'SEK', BGN'].
But in case unfiltered array does not contain all values in predifined list it should also order it correctly. For example : ["GBP", "EUR", "NOK", "LTU", "ZGN"] should look like ['EUR', 'NOK', 'GBP', 'LTU', 'ZGN'
I was trying to sort it using this function:
list.sort(c => ['EUR', 'USD', 'DKK', 'SKE', 'NOK', 'GBP'].indexOf(c))
but it puts all predefined currencies at the end of the list, not at the from. Maybe there is a better way of doing that?
.sort()takes to arguments - the first and second comparator. you can do this same thing but withlist.sort(( c, d ) => [...].indexOf(c) > [...].indexOf(d) ? 1 : [...].indexOf(c) < [...].indexOf(d) ? -1 : 0);but, adjust the>and<signs as needed.