This is a selection sort of an array of structs called folders, which has just two fields in it: a name field = nfield (type char and 75 char's long)
and a
points field = pfield. (unsigned long int type)
Where k -1 = the size of the array to be sorted.
In my program, this function is called repeatedly to sort small subsets of the whole array, which vary in size. It's blazing fast, but don't use it for 10's of thousands of records. That's for Qsort, and other top-notch sorting algorithm's.
Code:
void sortMilestoners(int k) {
int i, j;
char *name = malloc(75);
unsigned long int points;
points = 0; name = '\0';
for(i = 0; i < k-1; i++) {
for(j = i+1; j < k; j++) {
if(strcmp(folders[i].nfield, folders[j].nfield) > 0) {
name = '\0';
strcpy(name, folders[i].nfield);
folders[i].nfield[0] = '\0';
strcpy(folders[i].nfield, folders[j].nfield);
folders[j].nfield[0] = '\0';
strcpy(folders[j].nfield, name);
points = folders[i].pfield;
folders[i].pfield = folders[j].pfield;
folders[j].pfield = points;
}
}
}
putchar('\n');
for(i = 0; i < k; i++) {
fprintf(out, "\n%s, %lu", folders[i].nfield, folders[i].pfield);
/* for debug help */
// printf("\n%s, %lu", folders[i].nfield, folders[i].pfield);
// if(i % 20 == 0)
// sleep(5);
}
putchar('\n');
fprintf(out, "\n");
free(name);
}