tags:

views:

574

answers:

4

I need to sort some soccer standings. My problem is how to sort in the right order.

Sortorder:

  • Points DESC
  • Approximation match
  • Goal difference DESC
  • Goals scored DESC
  • Goals against

Input: TeamName - Points - GoalsScored - GoalsAgainst

  • Team 1 - 1 - 4 - 7
  • Team 2 - 5 - 8 - 6
  • Team 3 - 1 - 2 - 10
  • Team 4 - 8 - 12 - 5
  • Team 5 - 5 - 7 - 4

... Match #4 - Team 5 - Team 2 -- 1-2

Match #7 - Team 1 - Team 3 -- 3-3 ...

Output: TeamName - Points - GoalsScored - GoalsAgainst

  • Team 4 - 8 - 12 - 5
  • Team 2 - 5 - 8 - 6
  • Team 5 - 5 - 7 - 4
  • Team 1 - 1 - 4 - 7
  • Team 3 - 1 - 2 - 10

Because Team 2 won over Team 5 they ends up at 2nd place.

Because Team 1 draw against Team 3, they ends up at 4. place, with a better goal difference.

public class Standing
{
    public Team Team { get; set; }
    public int? MatchesPlayed { get; set; }
    public int? GoalsScored { get; set; }
    public int? GoalsAgainst { get; set; }
    public int? Points { get; set; }
}

public class Match
{
    public int MatchID { get; set; }
    public DateTime? PlayTime { get; set; }
    public Team HomeTeam { get; set; }
    public Team AwayTeam { get; set; }
    public int? HomeScore { get; set; }
    public int? AwayScore { get; set; }
}

public class Pool
{
    public int PoolID { get; set; }
    public string PoolName { get; set; }
    public DateTime? StartTime { get; set; }
    public List<Team> Teams { get; set; }
    public List<Match> Matches { get; set; }
    public List<Standing> Standings { get; set; }
}
+5  A: 

I think you should check out the IComparable interface and consider implementing it over your object(s).

No Refunds No Returns
A: 

You want to write yourself a comparator function and pass that to the sort method.

If one team has more points, indicate that that team is "bigger".

If both teams have the same number of points, look at the goal difference and use that to decide which team is "bigger".

Anon.
My problem is that i have to look at the match between the teams with the same score, before i can look at the goal difference.
Frets
So? Then do that. `if team1.score != team2.score return team1.score - team2.score; if team1.goaldiff != team2.goaldiff return team1.goaldiff - team2.goaldiff; return team1.goalsscored - team2.goalsscored;`
Anon.
+14  A: 

Can you use .NET 3.5? It's pretty straightforward to use the LINQ OrderBy and ThenBy extension methods for this.

Tim Robinson
Gets my vote. And in 2.0 / 3.0, LINQBridge (as long as you have VS2008 or above)
Marc Gravell
I think I like this better than my own answer.
No Refunds No Returns
Given that 2.0 has `List<T>.Sort(Comparison<T>)` and anonymous delegates, what would `OrderBy` and `ThenBy` improve here? It's fairly trivial to write a comparison predicate for a multi-field comparison.
Pavel Minaev
I find OrderBy/ThenBy a lot more readable than a sequence of if statement in a Comparison<T>. Theoretically there's no difference.
Tim Robinson
Agreed. 3.5 is not only about extensibility, but readability as well. Smarter, simpler, not unreadable.
LookitsPuck
Comparion<T> should be slightly more performant, since it doesn't have to maintain and compare prior sort subgroup information, and it doesn't generate the multiplicity of objects necessary to support that infrastructure. While I agree that readability is king, I would probably go with the IComparable version if this functionality is a key part of the application and if no RDBMS were available to handle the reporting functions of the application.
Jeffrey L Whitledge
I'm using VS2008 and .NET 3.5.Can you give an example with linq?My biggest problem is the approximation match.
Frets
+1  A: 

If your data is stored in a database (is it?) then you could sort in the select query by using ORDER BY. The resulting C# data structure will then be in order.

Norla