First of all, I saw some examples like:
But they don't work for me.
My struct is
typedef struct Team {
int total;
char name[N][N]; // Nombre
int points[N]; // Points
int pg[N]; // Won Matches
int pe[N]; // Tied Matches
int pp[N]; // Lost Matches
int gf[N]; // Goals For
int gc[N]; // Goals Against
} Teams;
int compareTeams(struct Team *e1, struct Team *e2){
int comparer;
int dg1 = e1->gf - e1->gc;
int dg2 = e2->gf - e2->gc;
//classified according to the points
if (e1->points > e2->points) {
comparer=-1;
} else if (e1->points < e2->points) {
comparer=1;
} else {
// with the same points, goal difference check
if (dg1 > dg2) {
comparer=-1;
} else if (dg1 < dg2) {
comparer=1;
} else {
// with the same goal difference , we check who scored more goals
if(e1->gf > e2->gf) {
comparer=-1;
} else if (e1->gf < e2->gf) {
comparer=1;
} else
comparer=0;
}
}
return comparer;
}
In main function, I have this:
Teams teams[100];
qsort(teams, teams->total, sizeof(struct Team), &compareTeams);
But obviously, it doesn't work :(