I have two arrays:
let x = ['a', 'b', 'c']
let y = ['z', 'c', 'a']
I want to sort y
based on the order defined in x
.
I have tried this approach:
y.sort((a,b) => { return x.indexOf(b) - x.indexOf(a) })
The problem is that it does not keep the unique order defined in x
let x = ['a', 'b', 'c']
let y = ['z', 'c', 'a']
y.sort((a,b) => { return x.indexOf(b) - x.indexOf(a) })
console.log(y);
I want to receive the output: ['a', 'c', 'z']
instead of ['c', 'a', 'z']