Top Ten Scorers8:54 with Carling Kirk
We'll determine the top ten scoring players using List.Sort and the IComparer interface.
We're ready to do our analysis of the top ten players in the MLS. 0:00 Let's make a method that will calculate the top ten players by points per game, 0:04 and return them in a list. 0:08 Public static Get TopTenPlayers, whoops. 0:13 And we'll give it a list of all the players, players. 0:22 We'll need to first sort the players by the points per game property in 0:33 descending order. 0:36 The list type has a method on it called sort, players.sort. 0:38 Let's check out the docs for it by hitting F1, here it is. 0:45 When you use the sort method with strings, it'll just sort them alphanumerical. 0:50 But since our list has objects, 0:54 we'll need to tell the sort method how to sort the elements in the list. 0:56 We can do that by using a comparer. 1:01 Let's click on that version here. 1:04 Sorts the elements in the entire list using the specified comparer. 1:08 Let's click on IComparer here. 1:13 Defines a method that a type implements to compare two objects. 1:17 IComparer is an interface which you might not have seen before. 1:22 An interface contains signatures of properties and methods for 1:26 which a class can implement. 1:29 It's like a blueprint. 1:31 It tells us how we should create our class. 1:32 So in order for us to implement this IComparer interface to use in our method, 1:35 we'd need to make sure that it has this method compare. 1:40 Let's check that one out, this method has two parameters of type T, 1:45 which in our case would be player and returns in int. 1:51 Back in our code, we can create a new class for 1:56 our comparer at Class and we'll call it 2:01 PlayerComparer We'll change this to public, 2:06 and we'll need to inherit from the IComparer interface, 2:14 IComparer of Player. 2:20 Notice that the IComparer of Player has a red squiggly line. 2:28 Visual Studio is telling us that our PlayerComparer 2:33 does not implement interface member IComparer Player, 2:36 because we haven't added the compare method that the interface defines. 2:40 So inside the class, we'll need to create a method named Compare 2:44 that matches the signature of the method in the interface. 2:48 We can use quick actions like we did before with adding the namespace, but 2:51 this time hitting Ctrl+period. 2:55 Here it says Implement interface, and 2:59 it even shows us a preview of the method, we'll choose that. 3:01 This method will return an integer based on the comparison between player X and 3:07 player Y. 3:12 Now we need to add the logic inside the method that determines which one is 3:13 greater than the other based on the points per game value of each player. 3:17 Let's go back to the docs for that compare method, so 3:22 that we can figure out what we need to return. 3:26 A signed integer that indicates the relative values of X and 3:29 Y, as shown in the following table. 3:32 If X is less than Y, we'd return a number less than zero like negative one. 3:35 If they're equal then we'd return zero. 3:41 And then if x is greater than y, then return something greater than zero, 3:43 like one. 3:47 Now back in our Compare method, 3:49 let's start with the first scenario where x is less than y. 3:51 If X.pointspergame is less than 3:58 y.pointspergame then will return negative one. 4:03 Then we'll do an else if x.pointspergame 4:12 is equal to y.pointspergame then return a zero 4:18 and one more else if x.pointspergame 4:27 is greater than y.pointspergame, then return one. 4:32 And down here at the end, we'll just return a 0 to make it happy. 4:42 This seems like it should work just fine, but I think there's a cleaner, 4:50 more concise way to determine this comparison. 4:54 All types at implement Icomparable like string and 4:57 and end in double have a method called compared to. 5:01 Our points per game property is a double, ao should have that method on it. 5:05 Let's check it out xpointspergame.compareto, 5:10 Compares this instance to a specified object and 5:16 returns an integer that indicates whether the value of this instance is less than 5:20 equal to or greater than the value of the specified object. 5:24 What a mouthful, but that is exactly what we need. 5:28 Now we can take out all of our logic and replace it with one line of code. 5:31 If x,pointspergamecompareto(y.pointspergame), 5:35 and not an if we just need to return 5:44 the result of the compare to method. 5:52 Let's get back to our top ten players and 5:54 we can call the list.sort with our new comparer. 5:57 Sort(new PlayerComparer()); And 6:01 return players; but we only want to return the first ten players. 6:10 So let's add a loop and a counter so 6:17 we can break out of the loop once we've printed out ten. 6:19 We'll need a new list to store our players. 6:22 var top ten players = new list of player. 6:24 We're missing a return type too. 6:33 Then in here, we' ll make ourselves a counter, int counter =0. 6:44 And then, a foreach ( var player in 6:50 players) then we'll add a player 6:56 to topTenPlayers.Add, a player. 7:01 Then we'll increment our counter, and once the counter gets to ten, 7:10 counter = 10 then we'll just break out of the loop. 7:16 Then we'll need to return topTenPlayers. 7:26 All right now up in our main method, we 7:31 can use this foreach loop to print all the players from our get topTenPlayers method. 7:34 var top ten players = get 7:40 toptenplayers and pass it all the players 7:44 then we'll need to copy/paste here for 7:53 each player in top ten players, and we'll right a little more to the console. 7:57 Let's right out Name, FirstName and then we'll print out the points. 8:05 We'll say PPG, for points per game. 8:16 Then the player.PointsPerGame. 8:23 All right let's run it, F5. 8:29 Uh-oh. 8:35 Looks like we've got a bug. 8:36 We sorted points per game ascending and not descending, so 8:38 we got the players with the least amount of points per game. 8:42 Why don't you take a moment after this video to see if you can fix this bug? 8:45 And in the next video I'll show you how I would do it. 8:50
You need to sign up for Treehouse in order to download course files.Sign up