This shows one of the drawbacks of using parallel arrays instead of the struct array. A simple solution would be to convert your code to use
struct {int index, num;} a[] = {{index1, num1},{index2, num2}, ...};
If your code base already makes heavy use of parallel arrays, you can write a view that knows about such difficulties. You can give it an array of arrays, not just one, and write a sort to perform swaps on each array, but only perform comparisons on the first. In other words:
void sort_pa(void **arrays, int len,int size, int(*cmp)(...)))
which you then used:
int a[] ={index1,index2,index3...indexI}; int b[] ={num1,num2,num3.......numI}; int *c[] = {b, a, NULL}; sort_pa(c, I, sizeof **c, cmp);
source share