I am trying to sort an array of structs with qsort. However I am having problems with understanding how to make the compare function. I have the following struct:
typedef struct {
char name[TEAM_SIZE];
int points;
} TEAM;
And I am trying to sort each team after its points with the highest first.
qsort(team, NUMBER_OF_TEAMS, sizeof(team), compare_points);
And the compare function:
int compare_points(const void *a, const void *b) {
TEAM *p1 = (TEAM *)a;
TEAM *p2 = (TEAM *)b;
if(p1->points < p2->points) {
return 1;
}
else {
return -1;
}
}
How would you do this?
Output:
Before:FCN 38
Before:FCM 59
Before:OB 46
Before:AGF 37
Before:AAB 50
Before:FCK 71
Before:HOB 18
Before:SDR 62
Before:RFC 47
Before:BIF 54
Before:EFB 30
Before:VFF 40
After:FCM 59
After 8
After:OB 46
After:AGF 37
After:AAB 50
After:FCK 71
After:HOB 18
After:SDR 62
After:RFC 47
After:BIF 54
After:EFB 30
After:VFF 40
sizeof(team)instead ofsizeof(TEAM)?