I don't think this really counts as magic
Code:
enum { SORT_BY_RESULT, SORT_BY_COUNT };
int sort_by;
int sort_results( const void *a, const void *b ) {
const prts *pa = a;
const prts *pb = b;
int result = 0;
switch ( sort_by ) {
case SORT_BY_RESULT:
if ( pa->result > pb->result ) result = -1;
if ( pa->result < pb->result ) result = 1;
break;
case SORT_BY_COUNT:
if ( pa->count > pb->count ) result = -1;
if ( pa->count < pb->count ) result = 1;
break;
}
return result;
}
Then you would do
sort_by = SORT_BY_RESULT; qsort() ; fprintf();
sort_by = SORT_BY_COUNT; qsort() ; fprintf();
To be honest, if you've only got a few functions to write, then copy/paste is probably just as good - and it saves messing around with globals.
But if your structure had like 10's of members, then we could get creative in different ways
> Why is the function working with "const prts *pa = a;" instead of "const datadump *pa = a;"?
prts is the type name, datadump is a variable name