All,
I have a structure defined below (I've eliminated some of the
elements because it's big). It's setup to be part of a single linked
list but at the moment I have an array built:
struct ENTRY linedata[MAX_ENTS];
I am populating a number of entries and want to sort on the field
linedata.transferPath. I'm using qsort and have built a comparator
function but cannot figure out how to specify the transferPath
element. If I use a simple character array and try to sort on a
column of that array, the following works (feel free to optimize it):
int comparePath (const void * a, const void * b)
{
char *a1 = (char *)a;
char *b1 = (char *)b;
char *a2 = &a1[TRANSTAB-1];
char *b2 = &b1[TRANSTAB-1];
return (strcmp((char *)a2, (char *)b2));
}
But, I want to sort on an element within a structure. I've tried, and
it fails. How can I specify the element to the comparator? (Darn
pointers!)? I haven't worked with qsort or comparator functions before.
int compareEntry (const void * a, const void * b)
{
char *a1 = (char *)a;
char *b1 = (char *)b;
char *a2 = a1.transferPath;
char *b2 = b1.transferPath;
return (strcmp((char *)a2, (char *)b2));
}
struct ENTRY {
struct ENTRY *next;
long int ruleCnt;
char ruleKey[5];
char *ruleKeyword;
char *ruleValue;
int labelCnt;
char labelKey[6];
char labelValue[2];
int junkCnt;
char junkKey[5];
int transferCnt;
char transferKey[9];
char *transferPath;
int header1Cnt;
char header1Key[8];
char *header1Value;
int verb1Cnt;
char verb1Key[5];
char *verb1Value;
int value1Cnt;
char value1Key[6];
char *value1Value;
int conjunctionCnt;
char conjunctionKey[12];
char *conjunctionValue;
int header2Cnt;
char header2Key[8];
char *header2Value;
int verb2Cnt;
char verb2Key[5];
char *verb2Value;
int value2Cnt;
char value2Key[6];
char *value2Value;
char *data;
};