I'm trying to sort a struct of data by team (data[i].team) but can't get it to work. I think I can use qsort but I can't really tell. If anyone will be able to help me ou
My struct :
struct store_race_data {
char race_name[MAX_LENGTH];
char first_name[MAX_LENGTH];
char last_name[MAX_LENGTH];
int age;
char team[MAX_LENGTH];
char nation[MAX_LENGTH];
char placement[MAX_LENGTH];
char race_time[MAX_LENGTH];
int point;
};
typedef struct store_race_data store_race_data;
My function. (Takes all cyclists from a total list and puts them in a new struct list that matches the strcmp)
void find_all_danes(store_race_data cyclist[], int n){
store_race_data *danes[MAX_LENGTH];
int i, k;
for (i=0; i<n-1; ++i) {
if ((strcmp(cyclist[i].nation, "DEN") == 0)) {
danes[k] = &cyclist[i];
++k;
}
}
printf("\n\t\t###### BEFORE SORTING ######\n");
print_cyclist_all_ptr(danes, k);
printf("\n\t\t###### AFTER SORTING ######\n");
qsort(*danes, k, sizeof(danes), &cmp_teams);
print_cyclist_all_ptr(danes, k);
}
My compare function
int cmp_teams(const void *a, const void *b){
const store_race_data *p1 = (store_race_data *) a,
*p2 = (store_race_data *) b;
return strcmp(p1->first_name, p2->first_name);
}