Say I have two vectors of identifiers - the second one being a shuffled version of the first one - like this:
using Random, Test
a = [ randstring(5) for _ in 1:20 ]
b = shuffle(a)
and now I want to loop through a such that the elements occur in the order given by b, i.e. I need a permutation v such that a[v] == b.
The solution I came up with is:
v = sortperm(a)[sortperm(sortperm(b))]
@test a[v] == b
Is there a more elegant than the above solution?
I do not care about performance right now - but I could imagine that the sortperm(sortperm(.)) construct will not behave well for longer vectors - so there might be also a more performant solution.
OK - a is intact not just a vector of identifiers but a vector of structs - the identifier is just one field in this struct. The order I want to go through them is given by a vector b of just these identifiers. The order is computed from data which is not presented in a, therefore I cannot just do a sort!(a, ...).
So therefore I need this permutation v and can for instance:
for k in 1:length(b)
do_something_with(a[v[k]])
end