I am trying to find out how (using a quicksort algorithm) to sort an struct array by 2 criterias. For example say I had a struct of:
struct employee{
char gender[12];
char name[12];
int id;
};
Say my input is:
struct employee arr[3]=
{
{"male","Matt",1234},
{"female","Jessica",2345},
{"male","Josh",1235}
};
I am wanting to sort the elements by gender first then the IDs in ascending order. An example would be have all the males printed first with their IDs in order and then all the females with theirs. I am trying to do this without using the qsort function but I havent the slightest idea how to check . Here is my sorting function:
void quicksort(struct employee *arr, int left, int right)
{
int pivot, l, r, temp;
if(left < right)
{
p = left;
l = left;
r = right;
while(l < r)
{
while(arr[l].id <= arr[p].id && l <= right)
l++;
while(arr[r].id > arr[p].id && r >= left)
r--;
if(l < r)
{
temp = arr[l].id;
arr[l].id = arr[r].id;
arr[r].id = temp;
}
}
temp = arr[r].id;
arr[r].id = arr[p].id;
arr[p].id = temp;
quicksort(arr, left, r-1);
quicksort(arr, r+1, right);
}
}
Any suggestions? I was thinking I could use strcmp but I cant figure out where to include it within the function.