Permute rows in "slices" of 3d array to match each other
Sorry I should have looked at your code sample; that was very informative.
Seems like this here gives an out-of-the-box solution to your problem:
Only really feasible for a few 100 points at most though, in my experience.