C# C# Streams and Data Processing Serialization Top Ten Scorers

Brandon Galde
Brandon Galde
11,252 Points

sharing my simple solution

I figured the easiest solution to the problem is to flip the 2 values you are comparing, ie. instead of x.CompareTo(y), I changed it to y.CompareTo(x), and this returned the desired result. also, Lists has a .Count on it, so instead of using a counter (with a "++" on it) you can just do: if (topTenPlayers.Count == 10) { break; }

4 Answers

Daniel Tkach
Daniel Tkach
7,607 Points

Let me share something too! public static List<Player> GetTopTenPlayers(List<Player> players) { var topTenPlayers = new List<Player>(); players.Sort(new PlayerComparer()); topTenPlayers = players.GetRange(0, 10); return topTenPlayers; }

i have made a pretty simple one . just in one single line: players.OrderByDescending(p => p.TotalPoints).ToList().GetRange(0, 10).ToArray();

Jeremy Moore
Jeremy Moore
21,586 Points

And yet another way ...

     return (
                from player in players
                orderby player.PointsPerGame descending
                select player
               )
               .Take(10)
               .ToList();

If you change the method return type to IEnumerable<Player> you can remove the "ToList()"