in reply to Rearranging Numbers

This is a special case of The Indirect Sort.

In the Indirect Sort, you have two arrays of items, and you want to sort the second array into the order that is defined by the first array.

(To get the two arrays, use split // on the two numbers.)

So the indirect sort method says to use this:

@order = split //, $order; @rearrangeme = split //, $rearrangeme; @indices = sort { $order[$a] <=> $order[$b] } (0..$digits-1); @answer = @rearrangeme[@indices];
quidity's solution above is simpler, but the Indirect Sort technique is like a recipe that you can apply without thinking whenever you have to arrange one array items into the order that is specified by a different array, so it's worth remembering.