Methods for Sorting Structures by Element...
Hi all.
I am trying to figure out sorting structures by their elements.
Let's say I have a group of structures of "person" type:
typedef struct person{
char f_name[50];
char l_name[50];
int age;
char state[50];
} p1,p2,p3,p4,(etc...);
Now my intent is to be able to sort all of the structures by any element.
I may want to sort all of the ages of all the structures and then display the entire structures in this order, or sort the states alphabetically and then display the entire structures in this order, so on and so on.
Now if these were mere arrays, this would not be anything difficult. In fact, if I have to assign each type of element to an array of common elements, then sort the arrays, this could be easily done. But then how to associate these results back to the original structs?
Let's say instead of my first declaration of new structs p1,p2, etc..., I created an array of structures. After this, I can use a loop to create an array of ages, an array of last names, of first names, of states containing all the data from all of the structs. So then I qsort each of these arrays, each by their own qsort method, ints and strings having their own "compare" function to the fourth parameter of qsort of course. But again, how do I relate this back to the entire structures? I have tried to think this problem out but I am basically stuck!
If someone could even just give me a theoretical solution to this, I think I can figure the rest.
Thanks.