B[sortperm(B)[invperm(sortperm(A))]]
i.e. sort array B and then follow backwords the sorting permutation of array A, to make B into A.
B[sortperm(B)[invperm(sortperm(A))]]
i.e. sort array B and then follow backwords the sorting permutation of array A, to make B into A.