Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
               )
               .Take(10)
               .ToList();

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