# Thread: Anyone knows how to sort arrays of structures?

1. ## Anyone knows how to sort arrays of structures?

Hie there...

Does anyone here knows how to sort arrays of structures in ascending and descending order?In the simplest way?

For example my structure lookes like this:
Code:
```struct Course{
char name[50];
char id[10];
};

struct Record{
char name[30];
long int id;
char year[10];
char major[10];
float gpa;
struct Course course_taken[6];
};```
If i declare struct Record student[10] ,how can i sort the members for all student[10] and display in ascending or descending ?Can i use Insertion sort?Coz that looks easier for a newbie like me, but i don't know how to use insertion sort to sort strings..

hope to get some reply here...thanks

2. Ascending and descending based on...?

why do I get :

The easiset way is using qsort.

4. ya, i cant access that link too...

Ascending and descending based on the type of member i want to sort.For example,if i want to sort a string ,then it is in alphabetical order...Any idea how to do that?

do i need to have a sorted data to use qsort?

5. How can i sort strings using insertion sort?Is it possible?Coz i only understand this simply sort and im running out of time now...pls help..thanks

6. Originally Posted by dude543

why do I get :

The easiset way is using qsort.
Because that thread, which was the first of this duplicate, appears to have been deleted. This was my original reply there:

7. Thanks Dave_Sinkula. Now I can see it.
> How can i sort strings using insertion sort?Is it possible?Coz i only understand this simply sort and im running out of time now...pls help..thanks

You can sort it anyway you like ( like insertion sort ). when you sort strings you might use
strcmp on two diffrent variables. On structs you should write your own compare function.

8. u mean using strcmp like say strcmp(s1,s2)<0 and strcmp(s1,s2)>0 ?? will it work if i do this with insertion sort?

9. Why are you set on insertion sort? Is it a requirement of your assignment? The function qsort is available in the standard library. If you want others you get to write them.

When using a function such as qsort, you pass in a pointer to a comparison function. In such a function would be a call to strcmp if the array of structures will be sorted on strings, and the result of this call would be the return value of the comparison function. From an old, unrelated bit of code:
Code:
```struct person
{
char name[32];
int  num;
};

int cmp_name(const void *a, const void *b)
{
const struct person *x = a, *y = b;
return strcmp(x->name, y->name);
}

/* ... */
qsort(in, count, sizeof *in, cmp_name);
/* ... */```
If you are doing it yourself, I would guess that you would likely have the comparison function in your sort preceding the swap.

Why not post the whole code of your attempt thus far?

10. Yes, but I thought qsort() wasn't stable.

11. And does that matter in this case?

12. Originally Posted by cdalten
Yes, but I thought qsort() wasn't stable.
Yes some implementations are not "stable". But that only means that if you sort your data multiple times with different compare-functions it doesn't preserve the order of the elements of a previous run if two elements compare equal. You never said anything about such a requirement before.
Kurt

13. Originally Posted by Dave_Sinkula
And does that matter in this case?
I guess it depends. If a person is going to use qsort(), they should be at least aware of some of the shortcomings in the function.

14. what are the shortcomings?

Coz i'll be doing a search and sort program on a student database.I need to sort alphabetically or numerically based on the results returned the search function.btw, my datas are not pre-sorted.So, can i still use quick sort?
Anyway, im studying on it now.....hehe...

15. Code:
```struct Course{
char name[50];
char id[10];
char year[10];
};

struct A{
char name[50];
char id[10];
};

struct Record{