Sorting an array according to other array
07:53 14 Apr 2011

I have an array with arrays of two elements. Now I would like to sort all values ending at zero (e.g. arr[3][0], arr[1][0]) to be sorted from low to high.

Then I would like to have the values ending at 1 (e.g. arr[2][1], arr[1][1]) to also be sorted, but not on its own order, but in the same order as the first array.

Here is what I tried:

int compareInts(const void* a, const void* b)
{
    return ( *(int*) a[0] - *(int*) b[0] );
}

int arr[4][2];

arr[0][0] = 50;
arr[0][1] = 0;

arr[1][0] = 40;
arr[1][1] = 1;

arr[2][0] = 50;
arr[2][1] = 2;

arr[3][0] = 85;
arr[3][1] = 3;

qsort( arr, 4, sizeof(int), compareInts );


I would like to have the following result:

arr[0][0] = 40;
arr[0][1] = 1;

arr[1][0] = 50;
arr[1][1] = 0;

arr[2][0] = 50;
arr[2][1] = 2;

arr[3][0] = 85;
arr[3][1] = 3;
c++ arrays sorting