I have 2 arrays
a = [2,3,1,4]
b = [{id: 1}, {id: 2}, {id: 3}, {id: 4}]
How do I get b sorted based on a? My desir
I am going to iterate on arcseldon's solution.
The right approach here is to flip the problem, instead of
"sorting a based on the order given by b",
I'd rather "enrich b with data coming from a"
This lets you maintain the time complexity of this function down to O(n),
instead of bringing it up to O(n2).
const pickById = R.pipe(
R.indexBy(R.prop('id')),
R.flip(R.prop),
);
const enrich = R.useWith(R.map, [pickById, R.identity]);
// =====
const data = [2, 3, 1, 4];
const source = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }];
console.log(
enrich(source, data),
);