I read the usage of qsort()
in <stdlib.h>
as described here qsort(). The syntax for the function is:
void qsort (void* base, size_t num, size_t size,
int (*compar)(const void*,const void*));
This function requires a sorting function compare()
as follows:
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
With this function, qsort() sorts elements of array in a ascending order. I am confused about the compare() function
, how does it help achieve ascending sorting. Anyone can explain the principle involved in a simple language? Thanks!
P.S. It is noted a similar question has been asked here cmpfunc in qsort() function in c