typedef struct { /* Record form for the data */ char name[24]; int id; char day[5]; short uses; } rec; typedef rec chunk[1000]; /* Array form for all data */ /* The compare function operates just like the strncmp library function. compare returns 0 if the specified number of characters is the same in the two strings, a value < 0 if the first string is less than the second and a value > 0 if the first string is greater than the second. compare is used instead of strncmp to avoid the need to include any .h (help) files. */ int compare (char *s, char *t, int num) { int j; for (j = 0; (j < num) && (*s == *t); s++,t++,j++) ; return (j == num ? 0 : (*s - *t)); } /* This is a simple exchange sort. Adjacent records are compared and if the first is greater than the second (based on the date characters), there is an exchange of records in the array. The sorting continues as long as there are exchanges occurring (as indicated by exch). */ void sort (chunk val, int *len) { int j = 1; /* Initialize outer loop index */ int k; int exch; /* Flag indicating an exchange */ rec thold; /* Temporary for the exchange */ do { exch = 0; k = 0; while (k < *len - j - 1) { if (compare(val[k].day, val[k+1].day, 5) > 0) { thold = val[k]; /* Make an exchange */ val[k] = val[k+1]; val[k+1] = thold; exch = 1; } k++; /* On to the next one */ } j++; } while (exch); /* If there was an exchange, keep going */ }