Hello all -
Is there any way to pass a parameter to the comparison function with qsort without using a global variable?
My goal is to sort the rows of a 2-D matrix based on a particular column. I have a working version that does use globals:
Is a better way to tell the comparison function which column to sort by?Code:static int nSortColumn = 0; /* global column index used by sortrows */
void sortrows(double** pMatrix,int nStartRow, int nStopRow, int nColumn)
{
nSortColumn = nColumn;
qsort((void*) (pMatrix+nStartRow),nStopRow-nStartRow+1,sizeof(double*),&sortrowsCompareFcn);
}
int sortrowsCompareFcn(const void * a,const void * b)
{
double** val1 = (double**) a;
double** val2 = (double**) b;
if(val1[0][nSortColumn] < val2[0][nSortColumn] )
{
return -1;
}
else if(val1[0][nSortColumn] > val2[0][nSortColumn] )
{
return 1;
}
else
{
return 0;
}
}
Thanks!