struct Record
{
char Surname[20];
char Initial;
unsigned short int Gender; //0 = male | 1 = female
unsigned short int Age;
};
Record X[100];
How can I use Quicksort to sort the values into increasing age, with females before males and surnames in alphabetical order? I’ve got a:
bool CompareData(const int& A, const int& B)
{
return Records[A].Age < Records[B].Age; //this sorts by age atm
}
This compares first by age and returns true if A should appear before B based on age.
If ages are equal, it then compares by gender, and returns true if A should appear before B based on gender (A is female and B is male).
If ages are equal and genders are equal, it then compares by surname (using
strcmp, although if you had usedstd::stringinstead of a char array, you could have just used<), and returns true if A should appear before B alphabetically by surname.