// Example of custom sorting of persons // First increasingly by age and in case of tie be alphabetical order // ---------------------------------- // Pedro Ribeiro (DCC/FCUP) - 04/10/2019 // ---------------------------------- #include #include #include #define MAX_STRINGS 500 #define MAX_LENGTH 30 // Type to store a person typedef struct { int age; char name[MAX_LENGTH+1]; // (+1) Space for '\0' } Person; // This function compares two elements of an array (needed for qsort) // It should return: // - number < 0 if "a" is smaller than "b" // - number > 0 if "a" is larger than "b" // - zero, if "a" and "b" are equal int comparePerson(const void *a, const void *b) { Person *p1 = (Person *)a; Person *p2 = (Person *)b; if (p1->age < p2->age) return -1; if (p1->age > p2->age) return +1; return strcmp(p1->name, p2->name); } int main() { int i, n; Person v[MAX_STRINGS]; scanf("%d", &n); for (i=0; i