I wanna sort an array of structures (struct Person) by score and it looks like this float PersonScore(Person *record, Person *query)
so I can't use qsort, I tried using qsort_r() but it passes for an implicit declaration when I compile. Any ideas?
typedef struct Person_t
{
char Nom[32];
char Prenom[32];
char AdresseEmail[64];
char Ville[96];
char Pays[64];
} Person;
static int compare_field(const char *record, const char *query, float *percentage) {
int DLev;
if (strlen(query) == 0) {
return 0;
}
DLev = DistanceLevenshtein ( record, query );
*percentage = (1.0f - DLev/((float)(MAX (strlen(record) , strlen(query))))) * 100.0f;
return 1;
}
float PersonScore( Person *record, Person *query) {
float total_match_percentage = 0, temp_percentage;
int total_match_fields = 0;
#define COMPAREFIELD(field) if (compare_field(record->field, query->field, &temp_percentage)) ({ total_match_percentage += temp_percentage; total_match_fields++; })
COMPAREFIELD (Prenom);
COMPAREFIELD (Nom);
COMPAREFIELD (AdresseEmail);
COMPAREFIELD (Ville);
COMPAREFIELD (Pays);
#undef COMPAREFIELD
return total_match_percentage / (float)total_match_fields;
}
// later in main
Personne query = {.Prenom = "Jamk", .Ville = "Pondon", .Pays = "United K"};
int thunk = 1;
Personne array[number_of_percentages];
printf("Before sorting the list is: \n");
for(int i = 0 ; i < number_of_percentages; i++ ) {
printf("%s ", array[i].Prenom);
}
qsort_r(array, number_of_percentages, sizeof(*array), compare_scores, &thunk);
printf("\nAfter sorting the list is: \n");
for(int i = 0 ; i < number_of_percentages; i++ ) {
printf("%s", array[i].Prenom);
}
}