I have this array ordered by hash[:points]
original = [{a: "Bill", points: 4}, {b: "Will", points: 3}, {c: "Gill", points: 2}, {d: "Pill", points: 1}]
I want to change the order of it's elements based on the order of a subset array, also ordered by hash[:points].
subset = [{c: "Gill", points: 2}, {b: "Will", points: 1}]
The subset's elements are always contained in the original. But the subset's length and order come at random. It may have two, three, or four elements, in any order.
I want to incorporate the order of the subset into the original array. This can be done by reordering the original, or recreating the original in the correct order. Either will do. But I don't want to merge them. The keys and values in the subset are not important, just the order of the elements.
For example, the above subset should produce this.
# Bill and Pill retain their original position
# Gill and Will swap places as per ordering of the subset
[{a: "Bill", points: 4}, {c: "Gill", points: 2}, {b: "Will", points: 3}, {d: "{Pill}", points: 1}]
Another example with this subset: [{c: "Gill", points: 3}, {b: "Will", points: 2}, {a: "Bill", points: 1}]
# Pill remains at index 3 since it was not in the subset
# Gill, Will, and Bill are reordered based on the order of the subset
[{c: "Gill", points: 3}, {b: "Will", points: 2}, {a: "Bill", points: 1}, {d: "Pill", points: 1}]
I've tried a bunch of stuff for the past couple of hours, but I'm finding this harder than it looks.
{b: 17}is not in the original..