1. ## qsort(): fcmp function

Code:
```typedef struct list
{
unsigned short int result;
unsigned short int count;
double score;
char name[22];
}prts;

int sort_results( const void *a, const void *b )
{
const prts *pa = a;
const prts *pb = b;

if ( pa->result > pb->result ) return -1;
if ( pa->result < pb->result ) return 1;
return 0;
}```
This is the fcmp function I am currently passing to qsort() to sort an array of structures.
My question is: How can I avoid writing a function for every variable of the structure by which I want to sort the array of structures?

Thank you very much for your help.

2. Which do you mean

- sort by result, and if result is equal sort by count
- sort by result then sometime later sort by count

3. There's really no way of getting around the comparisons. But you can avoid writing a different function for every variable by creating a new structure:

Code:
```typedef struct list
{
unsigned short int result;
unsigned short int count;
double score;
char name[22];
}prts;

typedef struct bleh
{
int varID; /* specific parameter you want to compare */
prts blah;
}BLEH;```
Then you can set the varID before you pass your new structures into the qsort. Then switch on the right variable in your compare function. This is just one way of doing it though.

Dunno if you mean to do this or not, but you're sorting on descending order.

4. sort by result then sometime later sort by count
Exactly! Sorry for not making this clear from the start.
qsort(result) --> fprintf --> qsort(count) --> fprintf ...

Dunno if you mean to do this or not, but you're sorting on descending order.
That's what I want. The high values are the interesting ones in this case.
Thank you for your help, Cshot. I will try your solution if Salem doesn't come up with some magic code.

Why is the function working with "const prts *pa = a;" instead of "const datadump *pa = a;"?

5. 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

6. I don't think this really counts as magic
At my current skill level this IS magic!

Code:
```int result = 0;
...
return result;```
Why? I thought I am accessing the variable result in the array of structures? And why 'return'? I am a little bit confused.

But if your structure had like 10's of members, then we could get creative in different ways

Well, the structure is still in development and the number of variables by which to sort might well exceed 10 ... maybe it will be about 15 to 20. If you've something creative in mind then please let me know!

prts is the type name, datadump is a variable name
Okay! Thnx. I've thought about it again - now it's clear.

7. > Why? I thought I am accessing the variable result in the array of structures?
You are - it's just a bad choice of variable name

result and pa->result are two different things

> int result = 0;
qsort cmp() functions return -1, 0, +1
I'm just guessing at the answer so there are only two left to worry about in the code.

Then I set result to either +1 or -1 depending on the comparison

> And why 'return'? I am a little bit confused.
Huh?
You had returns in your code - this just returns the contents of a local var rather than a compile-time constant.

> If you've something creative in mind then please let me know!
When you are ready, I will

8. result and pa->result are two different things

Now I got it! At first glance it was a little bit disturbing.
Thanks for making this clear.

> If you've something creative in mind then please let me know!

When you are ready, I will

Okay! I will continue to develop my program and come back to you when I've the final structure ... although it will not remain unchanged in the future. The structure will be updated as I add new calculations and thus new variables by which to sort.