Top Ten Players
Let's make a method that will calculate the top ten players by points per game and return them in a list.
public static List<Player> GetTopTenPlayers(List<Player> players)
{
return
}
To compare players by their points per game, we could create another class to compare the items.
Create new class named PlayerComparer.
public class PlayerComparer : IComparer<Player>
{
}
We will see a red underline. Just right-click > Quick Actions and Refractoring. And here they are.
public class PlayerComparer : IComparer<Player>
{
public int Compare(Player x, Player y)
{
throw new NotImplementedException();
}
}
Now, we want to compare point per game of player x and player y. We could just type.
public class PlayerComparer : IComparer<Player>
{
public int Compare(Player x, Player y)
{
return x.PointsPerGame.CompareTo(y.PointsPerGame);
}
}