# Urgent Help - Qsort

This is a discussion on Urgent Help - Qsort within the C Programming forums, part of the General Programming Boards category; Hi I am trying to qsort a struct, here is my code for my struct: Code: struct pers { char ...

1. ## Urgent Help - Qsort

Hi I am trying to qsort a struct, here is my code for my struct:

Code:
```struct pers
{
char *name;
int age;
};

typedef struct queue
{
int number;
struct pers person[400];
}cc;

cc p[21];```
I have trying to work this out for days, but cant get my head around it.
At this point the "struct pers person[400];" contains data which is linked to the struct pers, i need to use a qsort to sort [asc] based on the persons age.

Please any suggestions? You help would be greatly appreciated.

Thanks

2. Originally Posted by it01y2
I have trying to work this out for days, but cant get my head around it.
Show us something you've tried and someone will tell you why it doesn't work. Or ask a more specific question about the compare function you use with qsort (that is, the 4th argument ). I'm not going to write the code for you.

3. You can begin with showing us the comparison function you're passing to qsort.

4. ## Qsort function

This is my qsort function:
Code:
```for(j=0; j < 22; j++)
{
qsort(p[j].person,p[j].number,sizeof(struct queue),struct_cmp);
}```
And my (struct_cmp) comparison function consists of:

Code:
```int struct_cmp(const void *a, const void *b) /* Compare integer function */
{
cc *p1 = (cc*)a;
cc *p2 = (cc*)b;

if(p1->person->age > p2->person->age)
return 1;
else
return 0;
}```
Im guessing this is total rubbish, i just get stuck with pointers and structs.

5. Okay, qsort arguments are (your second one is definitely wrong):

1) a pointer to your struct array
2) the number of elements in the array (how bout 400?)
3) the sizeof one element
4) the compare function pointer

If you are using a struct array, you cannot use a compare function which accepts two ints. It must accept two structs and compare the relevent fields within them.

6. ## Qsort function

Originally Posted by MK27
Okay, qsort arguments are (your second one is definitely wrong):

1) a pointer to your struct array
2) the number of elements in the array (how bout 400?)
3) the sizeof one element
4) the compare function pointer

If you are using a struct array, you cannot use a compare function which accepts two ints. It must accept two structs and compare the relevent fields within them.
Thanks for the tips dude, really appreciate it. Its just implementing this now, I will give it a go.

7. ## Point 3

Code:
`qsort(q[j].person,400,sizeof(struct queue),struct_cmp);`
I am not sure how to get the sizeof one element?

8. Originally Posted by it01y2
Code:
`qsort(q[j].person,400,sizeof(struct queue),struct_cmp);`
I am not sure how to get the sizeof one element?
sizeof(struct pers)? I am not awake yet...

9. Originally Posted by it01y2
Code:
`qsort(q[j].person,400,sizeof(struct queue),struct_cmp);`
I am not sure how to get the sizeof one element?
Since q[j].person is an array of struct pers, then each element is sizeof(struct pers), not sizeof(struct queue). qsort needs to know how big each unit of the array is, since the pointer (1st arg) is a void type, so the "kind" of array cannot be determined by qsort internally. It has to send 2X "3rd arg" bytes of data at a time to the compare function.

KK i see, ty

11. ## Qsort function

In the function im going to have to pass the struct by pointer into the compare function, then access the childs to do a comparison im guessing so?

12. Originally Posted by it01y2
In the function im going to have to pass the struct by pointer into the compare function, then access the childs to do a comparison im guessing so?
Add a cast to the correct type and I think that you are there...

God I hope my caffeine kicks in soon...

13. *shrug* I really dont understand this, how would you do it?

14. ## Sorting doesn't work

I have tried to create the compare function but im doing something wrong:

Code:
```int struct_cmp(const void *a, const void *b) /* Compare integer function */
{
struct pers *ia = (struct pers *)a;
struct pers *ib = (struct pers *)b;
if(ia->age > ib->age)
return -1;
else
return 1;
}```
Is this correct?

Thanks

15. Originally Posted by it01y2
I have tried to create the compare function but im doing something wrong:
I would stick some printf's in there, or use a debugger, to check the values and see what's happening.

Page 1 of 2 12 Last