If I understand the problem correctly, then this is a pretty good answer. I would just like to add that, in the wild (not just a school exercise), this sort of thing arises when the first array contains small keys, and the second array contains significantly more heavy-weight data (larger and/or more complex).

In that case, it is better to create an array of key,index or key,ref pairs and sort that; then use the sorted keys to access the values.