Given a structure array (in C) I am attempting to print out the results in groups of gender and in sub order by numerical order. For example:
struct employee{
char gender[13]
char name[13];
int id;
};
Say I define the structure array like so:
struct employee info[2]={{"male","Matt",1234},{"female","Jessica",2345},{"male","Josh",1235}};
How could I go about printing the results like
1234 Matt
1235 Josh
2345 Jessica
You’ll need to implement a sorting function that compares the structs as you require
And then use qsort from the standard library.
Inside the compare function you may want to check for id being equal, then you can sort by name (also using
strcmp()) however you like.Edit: Just compiled and fixed this up. Here’s a little test program
With output: