#include #include struct ChargeList { int Section_ID; int SubSection_ID; int SeqNo; char Sn_Desc[10]; char TT_Short_Desc[10]; char TZ_Short_Desc[10]; char OutgoingTime[13]; char IncomingTime[13]; }; struct ChargeList the_list[30000]; int Compare2(struct ChargeList **e1,struct ChargeList **e2) { int diff; if ((*e1)->Section_ID > (*e2)->Section_ID) { return 1; } else if ((*e1)->Section_ID == (*e2)->Section_ID) { if ((*e1)->SubSection_ID > (*e2)->SubSection_ID) { return 1; } else if ((*e1)->SubSection_ID == (*e2)->SubSection_ID) { if ((*e1)->SeqNo > (*e2)->SeqNo) { return 1; } else if ((*e1)->SeqNo == (*e2)->SeqNo) { diff = strcmp((*e1)->TT_Short_Desc,(*e2)->TT_Short_Desc); if (diff > 0) { return 1; } else if (diff == 0) { diff = strcmp((*e1)->TZ_Short_Desc,(*e2)->TZ_Short_Desc); if (diff > 0) { return 1; } else if (diff == 0) { diff = strcmp((*e1)->IncomingTime,(*e2)->IncomingTime); if (diff > 0) { return 1; } else if (diff == 0) { return 0; } else { return -1; } } else { return -1; } } else { return -1; } } else { return -1; } } else { return -1; } } else { return -1; } } int Compare(struct ChargeList *e1,struct ChargeList *e2) { int diff; if (e1->Section_ID > e2->Section_ID) { return 1; } else if (e1->Section_ID == e2->Section_ID) { if (e1->SubSection_ID > e2->SubSection_ID) { return 1; } else if (e1->SubSection_ID == e2->SubSection_ID) { if (e1->SeqNo > e2->SeqNo) { return 1; } else if (e1->SeqNo == e2->SeqNo) { diff = strcmp(e1->TT_Short_Desc,e2->TT_Short_Desc); if (diff > 0) { return 1; } else if (diff == 0) { diff = strcmp(e1->TZ_Short_Desc,e2->TZ_Short_Desc); if (diff > 0) { return 1; } else if (diff == 0) { diff = strcmp(e1->IncomingTime,e2->IncomingTime); if (diff > 0) { return 1; } else if (diff == 0) { return 0; } else { return -1; } } else { return -1; } } else { return -1; } } else { return -1; } } else { return -1; } } else { return -1; } } main() { int i,j,k,l,m = 0; long int fake_time = 200112100212; struct ChargeList **templist; for (i = 0; i < 5; i++) { for (k = 99999; k > 99750; k--) { for (j = 0; j < 11; j++) { the_list[m].Section_ID = i; the_list[m].SubSection_ID = m % 2; the_list[m].SeqNo = k; if ((m % 3) == 0) strcpy(the_list[m].Sn_Desc, "TEL"); else if ((m % 3) == 1) strcpy(the_list[m].Sn_Desc, "SMS"); else strcpy(the_list[m].Sn_Desc, "XYZ"); if ((m % 2) == 0) strcpy(the_list[m].TT_Short_Desc,"PEAK"); else strcpy(the_list[m].TT_Short_Desc,"OFFPEAK"); if ((m % 7) == 0) strcpy(the_list[m].TZ_Short_Desc,"VC1"); else if ((m % 7) == 1) strcpy(the_list[m].TZ_Short_Desc,"VC2"); else if ((m % 7) < 4) strcpy(the_list[m].TZ_Short_Desc,"VC3"); else strcpy(the_list[m].TZ_Short_Desc,"VC4"); sprintf(the_list[m].IncomingTime,"%ld",fake_time - 5); sprintf(the_list[m].OutgoingTime,"%ld",fake_time); fake_time += 7; m++; } } } printf("I'm here\n"); templist = malloc(m * sizeof(struct ChargeList *)); for (i = 0; i < m; i++) { templist[i] = &the_list[i]; } printf("I'm here 2\n"); // qsort(&the_list[0],m,sizeof(struct ChargeList),Compare); qsort(templist,m,sizeof(struct ChargeList *),Compare2); printf("I'm here too\n"); for (l = 0; l < m; l++) { printf("Section_ID %d, SubSection_ID %d, SeqNo %d, Sn_Desc '%s', " "TT_Short_Desc '%s', TZ_Short_Desc '%s' " "IncomingTime '%s, OutgoingTime '%s'\n", templist[l]->Section_ID, templist[l]->SubSection_ID, templist[l]->SeqNo, templist[l]->Sn_Desc, templist[l]->TT_Short_Desc, templist[l]->TZ_Short_Desc, templist[l]->IncomingTime, templist[l]->OutgoingTime); } #if 0 for (l = 0; l < m; l++) { printf("Section_ID %d, SubSection_ID %d, SeqNo %d, Sn_Desc '%s', " "TT_Short_Desc '%s', TZ_Short_Desc '%s' " "IncomingTime '%s, OutgoingTime '%s'\n", the_list[l].Section_ID, the_list[l].SubSection_ID, the_list[l].SeqNo, the_list[l].Sn_Desc, the_list[l].TT_Short_Desc, the_list[l].TZ_Short_Desc, the_list[l].IncomingTime, the_list[l].OutgoingTime); } #endif }