I want to use the built-in qsort function (stdlib.h) to sort a two dimensional array say arr[10][2];
could someone help me write the compare function if arr[m][] goes before arr[n][] if arr[m][0]<arr[n][0]
Printable View
I want to use the built-in qsort function (stdlib.h) to sort a two dimensional array say arr[10][2];
could someone help me write the compare function if arr[m][] goes before arr[n][] if arr[m][0]<arr[n][0]
Two-dim arrays are tricky there...
Code:// call qsort assuming arr[] is integer
qsort(&arr,10,sizeof(arr[0][0])*2, cmp)
int cmp(void *a, void *b){
return ( (int*)a - (int*)b);
}
Expected something along those lines, but I was too tired to think properly about it.
But since he wants to compare the first element of the subarrays this
return ( (int*)a - (int*)b);
should be
return ( (*(int*)a) - (*(int*)b));