In my app I want to show football tables from different leagues for example the English Premier League and the Spanish Primera Division.
I created a SeasonTableTeamObject where I save all values. (TeamID, points, goals, conceded goals, playedGames, wins, draws, defeats)
public class SeasonTableTeamObject
{
public int Points;
public int Goals;
public int AgainstGoals;
public int TeamID;
public string Teamname;
public int PlayedGames;
public int WinGames;
public int DrawGames;
public int LostGames;
public int Rank;
public int GoalDifference => (Goals - AgainstGoals);
public SeasonTableTeamObject(int points, int goals, int againstGoals, int team, string teamName)
{
Points = points;
Goals = goals;
AgainstGoals = againstGoals;
TeamID = team;
Teamname = teamName;
PlayedGames = 0;
WinGames = 0;
DrawGames = 0;
LostGames = 0;
}
}
Then I created a list with these SeasonTableTeamObjects for each league. (Each club in this league has its own SeasonTableTeamObject)
Now comes the tricky part: these leagues have different rules. I created a variable inside the league class to distinguish between the rule sets.
public class League
{
public List<Match> ListOfAlreadyPlayedMatches;
public List<SeasonTableTeamObject> SeasonTableList;
public int Rules;
}
public class Match
{
public int HomeTeamId;
public int AwayTeamId;
public int WinnerTeamId;
}
In the Premier League you sort the table like this:
- Points
- Goal difference
Goals
SeasonTableList.OrderByDescending(p => p.Points).ThenByDescending(p => p.GoalDifference).ThenByDescending(p => p.Goals).ToList();
In the Spanish Primera Division:
- Points
- Direct comparison (without away goals rule)
- Goal difference
Direct comparison means the result of the matches between the clubs that have the same amount of points. For example Real Madrid and Barcelona have both 20 Points. Then you have to compare the matches between these two clubs. (I created a WinnderTeamId in Match to do this. I assume that there must be a winner in each game for simplification).
Questions: It was no problem to sort the list by points, goals etc, but how can I achieve the behavior of the spanish league. (I have a list of matches
where I can find the specific games, but I have no idea how to do this. I read something about the IComparable
and the IComparer
but I don't know if this is the correct way to reach my goals...)