>> for instance, sort first by name, then by score; or by last name then first name.
Why would it make sense to sort by name twice?
Have you considered using qsort in any way? It may be simpler just to focus on how elements compare to one another rather than writing a new, untested sort, especially since the sorting is so complex.
Code:
struct sorter {
struct record * data;
unsigned int index;
};
void
filtered_sort ( struct record * list, unsigned int * parallel, unsigned int nelem ) {
struct sorter * temp = make_it ( list, nelem );
if ( temp ) {
unsigned int i;
for ( i = 0; i < nelem-1; i++ ) {
temp[i].data = &list[i];
temp[i].index = i;
}
qsort( temp, sizeof temp / sizeof temp[0], sizeof temp[0], filter_sort );
for ( i = 0; i < nelem-1; i++ ) { parallel[i] = temp[i].index; }
free( temp );
temp = 0;
}
}
Assuming make_it returns an array of sorter objects the correct size, I think this is some okay pseudo-code... just figure out how to write a comparison function that
- attempts to sort by last name
- then first name
- then by score
trying to minimize as many "ties" as possible.
The parallel array filtered_sort fills can be used to access an array of unsorted records.