It would be simpler to sort array_order, using a[array_order[...]] for the compares and pivot points. Then afterwards, you can reorder or copy a according to array_order. The copy is simple, using i to index through the array, b[i] = a[array_order[i]]; . The in place reorder involves a variation of cycle sort (vA is the array, vI is the array of sorted indices):
Code:
void reorder(vector<int>& vA, vector<size_t>& vI)
{
size_t i, j, k;
int t;
for(i = 0; i < vA.size(); i++){
if(i != vI[i]){
t = vA[i];
k = i;
while(i != (j = vI[k])){
// every move places a value in it's final location
vA[k] = vA[j];
vI[k] = k;
k = j;
}
vA[k] = t;
vI[k] = k;
}
}
}