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! While you're at it, check out some resources Treehouse students have shared here.
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 trialBrandon Galde
12,741 Pointssharing 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
Jeremy McLain
Treehouse Guest TeacherYep! That's another way you can do it!
Daniel Tkach
7,608 PointsLet 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; }
Adham Ali
Courses Plus Student 1,112 Pointsi have made a pretty simple one . just in one single line: players.OrderByDescending(p => p.TotalPoints).ToList().GetRange(0, 10).ToArray();
Jeremy Moore
21,586 PointsAnd 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()"