I need to sort my list using the selected array.
const list = [
{ name: 'BMW' },
{ name: 'AUDI' },
{ name: 'MINI' },
{ name: 'FIAT' },
{ name: 'KIA' },
]
const selected = [ 'MINI', 'KIA' ]
What is the best way to do this? I have found some examples but not too sure how to adapt this to work the way I want it to!
list.sort((a,b) => (a.last_nom > b.last_nom) ? 1 : ((b.last_nom > a.last_nom) ? -1 : 0))
Expected result:
const list = [
{ name: 'MINI' },
{ name: 'KIA' },
{ name: 'BMW' },
{ name: 'AUDI' },
{ name: 'FIAT' },
]
last_nombename?selected.indexOf(a.name)andselected.indexOf(b.name), and compare those before comparing the values ofa.nameandb.nameselectedjust has some ofthem.