How can I sort the elements of the list A so that they follow the ordering of another (superset) list B? Assume no duplicates.
E.g. A might contain [8 2 5 1] and B might contain [5 6 9 8 7 4 1 2 3], and so I’d like to sort A to become [5 8 1 2]
I’m interested in ways of doing this efficiently and with good runtime complexity.
If B is a superset of A, I’d just dump A into a hash-table, scan B and create a new list where I insert every element from B that is contained in the hash-table. Uses O(a) extra memory and O(b) runtime.