Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Brandon Galde
Brandon Galde
12,741 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,608 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

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