ok new problem.

i have the following code to perform an exchange sort

Code:

void sortRecords(RECORD * ptrDatabase, int * counter) {
// Setup variables used within function
char temp[5];
RECORD * ptrNew, * ptrNew2, * prevNode, * tempRecord;
int dd1, mm1, yy1, dd2, mm2, yy2;
long elap;
ptrNew = ptrDatabase; // Make ptrNew = ptrDatabase so ptrDatabase is not modified.
// Run through all records - 1
while (ptrNew->next!=(RECORD*) NULL){
prevNode = ptrNew;
ptrNew = ptrNew->next; /* first item doesn't contain data, last does */
if(ptrNew->next == NULL) break;
// Get the dateAdded string and turn it into integers for processing
temp[0] = ptrNew->data.dateAdded[0];
temp[1] = ptrNew->data.dateAdded[1];
temp[2] = '\0';
dd1 = atoi(temp);
temp[0] = ptrNew->data.dateAdded[3];
temp[1] = ptrNew->data.dateAdded[4];
temp[2] = '\0';
mm1 = atoi(temp);
temp[0] = ptrNew->data.dateAdded[6];
temp[1] = ptrNew->data.dateAdded[7];
temp[2] = ptrNew->data.dateAdded[8];
temp[3] = ptrNew->data.dateAdded[9];
temp[4] = '\0';
yy1 = atoi(temp);
ptrNew2 = ptrNew->next;
// Run through all records after the current pass number
while (ptrNew2->next!=(RECORD*) NULL){
// Get the dateAdded string and turn it into integers for processing
temp[0] = ptrNew2->data.dateAdded[0];
temp[1] = ptrNew2->data.dateAdded[1];
temp[2] = '\0';
dd2 = atoi(temp);
temp[0] = ptrNew2->data.dateAdded[3];
temp[1] = ptrNew2->data.dateAdded[4];
temp[2] = '\0';
mm2 = atoi(temp);
temp[0] = ptrNew2->data.dateAdded[6];
temp[1] = ptrNew2->data.dateAdded[7];
temp[2] = ptrNew2->data.dateAdded[8];
temp[3] = ptrNew2->data.dateAdded[9];
temp[4] = '\0';
yy2 = atoi(temp);
elap = days(dd1, mm1, yy1, dd2, mm2, yy2); // Work out how many days have passed between the two dates
// If elap is negitive then it must be moved.
if ( elap < 0 ) {
tempRecord->next = ptrNew->next;
ptrNew->next = ptrNew2->next;
ptrNew2->next = tempRecord->next;
prevNode->next = ptrNew2;
ptrNew2 = ptrNew->next;
ptrNew = prevNode->next;
//ptrNew = prevNode->next;
//ptrNew2 =
/*ptrNew->next = ptrNew2->next;
ptrNew2->next = ptrNew;
prevNode->next = ptrNew2;*/
}
else {
ptrNew2 = ptrNew2->next; /* first item doesn't contain data, last does */
}
}
}
}

However it does not sort the data propperly. I am guess its because when I do a swap of the records i am messing up when I acutually am with the sort on a whole. eg. ptrNew is much further down the list than it should be. its kinda hard to explain. If you have a look at the code though I think you should be able to see what i mean.

Thanks for your help

Chris