1 00:00:00,710 --> 00:00:03,820 We're ready to do our analysis of the top ten players in the MLS. 2 00:00:04,910 --> 00:00:08,761 Let's make a method that will calculate the top ten players by points per game, 3 00:00:08,761 --> 00:00:10,041 and return them in a list. 4 00:00:13,221 --> 00:00:21,152 Public static Get TopTenPlayers, whoops. 5 00:00:22,590 --> 00:00:30,420 And we'll give it a list of all the players, players. 6 00:00:33,250 --> 00:00:36,620 We'll need to first sort the players by the points per game property in 7 00:00:36,620 --> 00:00:37,340 descending order. 8 00:00:38,900 --> 00:00:43,994 The list type has a method on it called sort, players.sort. 9 00:00:45,790 --> 00:00:50,320 Let's check out the docs for it by hitting F1, here it is. 10 00:00:50,320 --> 00:00:54,450 When you use the sort method with strings, it'll just sort them alphanumerical. 11 00:00:54,450 --> 00:00:56,840 But since our list has objects, 12 00:00:56,840 --> 00:01:01,050 we'll need to tell the sort method how to sort the elements in the list. 13 00:01:01,050 --> 00:01:02,910 We can do that by using a comparer. 14 00:01:04,210 --> 00:01:05,824 Let's click on that version here. 15 00:01:08,795 --> 00:01:12,070 Sorts the elements in the entire list using the specified comparer. 16 00:01:13,530 --> 00:01:15,520 Let's click on IComparer here. 17 00:01:17,955 --> 00:01:21,279 Defines a method that a type implements to compare two objects. 18 00:01:22,640 --> 00:01:26,230 IComparer is an interface which you might not have seen before. 19 00:01:26,230 --> 00:01:29,650 An interface contains signatures of properties and methods for 20 00:01:29,650 --> 00:01:31,540 which a class can implement. 21 00:01:31,540 --> 00:01:32,710 It's like a blueprint. 22 00:01:32,710 --> 00:01:35,510 It tells us how we should create our class. 23 00:01:35,510 --> 00:01:40,750 So in order for us to implement this IComparer interface to use in our method, 24 00:01:40,750 --> 00:01:44,140 we'd need to make sure that it has this method compare. 25 00:01:45,800 --> 00:01:51,410 Let's check that one out, this method has two parameters of type T, 26 00:01:51,410 --> 00:01:54,510 which in our case would be player and returns in int. 27 00:01:56,180 --> 00:02:01,525 Back in our code, we can create a new class for 28 00:02:01,525 --> 00:02:06,168 our comparer at Class and we'll call it 29 00:02:06,168 --> 00:02:14,901 PlayerComparer We'll change this to public, 30 00:02:14,901 --> 00:02:20,144 and we'll need to inherit from the IComparer interface, 31 00:02:20,144 --> 00:02:26,265 IComparer of Player. 32 00:02:28,700 --> 00:02:32,050 Notice that the IComparer of Player has a red squiggly line. 33 00:02:33,820 --> 00:02:36,510 Visual Studio is telling us that our PlayerComparer 34 00:02:36,510 --> 00:02:40,170 does not implement interface member IComparer Player, 35 00:02:40,170 --> 00:02:43,520 because we haven't added the compare method that the interface defines. 36 00:02:44,520 --> 00:02:48,000 So inside the class, we'll need to create a method named Compare 37 00:02:48,000 --> 00:02:51,410 that matches the signature of the method in the interface. 38 00:02:51,410 --> 00:02:55,630 We can use quick actions like we did before with adding the namespace, but 39 00:02:55,630 --> 00:02:59,500 this time hitting Ctrl+period. 40 00:02:59,500 --> 00:03:01,710 Here it says Implement interface, and 41 00:03:01,710 --> 00:03:05,310 it even shows us a preview of the method, we'll choose that. 42 00:03:07,550 --> 00:03:12,110 This method will return an integer based on the comparison between player X and 43 00:03:12,110 --> 00:03:13,640 player Y. 44 00:03:13,640 --> 00:03:17,530 Now we need to add the logic inside the method that determines which one is 45 00:03:17,530 --> 00:03:22,210 greater than the other based on the points per game value of each player. 46 00:03:22,210 --> 00:03:26,290 Let's go back to the docs for that compare method, so 47 00:03:26,290 --> 00:03:29,130 that we can figure out what we need to return. 48 00:03:29,130 --> 00:03:32,290 A signed integer that indicates the relative values of X and 49 00:03:32,290 --> 00:03:34,210 Y, as shown in the following table. 50 00:03:35,680 --> 00:03:41,000 If X is less than Y, we'd return a number less than zero like negative one. 51 00:03:41,000 --> 00:03:43,830 If they're equal then we'd return zero. 52 00:03:43,830 --> 00:03:47,340 And then if x is greater than y, then return something greater than zero, 53 00:03:47,340 --> 00:03:47,880 like one. 54 00:03:49,460 --> 00:03:51,462 Now back in our Compare method, 55 00:03:51,462 --> 00:03:55,183 let's start with the first scenario where x is less than y. 56 00:03:58,610 --> 00:04:03,105 If X.pointspergame is less than 57 00:04:03,105 --> 00:04:10,010 y.pointspergame then will return negative one. 58 00:04:12,540 --> 00:04:18,032 Then we'll do an else if x.pointspergame 59 00:04:18,032 --> 00:04:24,141 is equal to y.pointspergame then return a zero 60 00:04:27,074 --> 00:04:32,076 and one more else if x.pointspergame 61 00:04:32,076 --> 00:04:39,276 is greater than y.pointspergame, then return one. 62 00:04:42,537 --> 00:04:48,131 And down here at the end, we'll just return a 0 to make it happy. 63 00:04:50,420 --> 00:04:54,310 This seems like it should work just fine, but I think there's a cleaner, 64 00:04:54,310 --> 00:04:57,860 more concise way to determine this comparison. 65 00:04:57,860 --> 00:05:01,320 All types at implement Icomparable like string and 66 00:05:01,320 --> 00:05:05,730 and end in double have a method called compared to. 67 00:05:05,730 --> 00:05:10,000 Our points per game property is a double, ao should have that method on it. 68 00:05:10,000 --> 00:05:16,539 Let's check it out xpointspergame.compareto, 69 00:05:16,539 --> 00:05:20,090 Compares this instance to a specified object and 70 00:05:20,090 --> 00:05:24,220 returns an integer that indicates whether the value of this instance is less than 71 00:05:24,220 --> 00:05:28,070 equal to or greater than the value of the specified object. 72 00:05:28,070 --> 00:05:31,650 What a mouthful, but that is exactly what we need. 73 00:05:31,650 --> 00:05:35,274 Now we can take out all of our logic and replace it with one line of code. 74 00:05:35,274 --> 00:05:44,234 If x,pointspergamecompareto(y.pointspergame), 75 00:05:44,234 --> 00:05:49,357 and not an if we just need to return 76 00:05:52,150 --> 00:05:54,272 the result of the compare to method. 77 00:05:54,272 --> 00:05:57,472 Let's get back to our top ten players and 78 00:05:57,472 --> 00:06:01,466 we can call the list.sort with our new comparer. 79 00:06:01,466 --> 00:06:10,193 Sort(new PlayerComparer()); And 80 00:06:10,193 --> 00:06:17,150 return players; but we only want to return the first ten players. 81 00:06:17,150 --> 00:06:19,040 So let's add a loop and a counter so 82 00:06:19,040 --> 00:06:21,090 we can break out of the loop once we've printed out ten. 83 00:06:22,590 --> 00:06:24,626 We'll need a new list to store our players. 84 00:06:24,626 --> 00:06:33,036 var top ten players = new list of player. 85 00:06:33,036 --> 00:06:38,605 We're missing a return type too. 86 00:06:44,382 --> 00:06:50,790 Then in here, we' ll make ourselves a counter, int counter =0. 87 00:06:50,790 --> 00:06:56,001 And then, a foreach ( var player in 88 00:06:56,001 --> 00:07:01,041 players) then we'll add a player 89 00:07:01,041 --> 00:07:06,441 to topTenPlayers.Add, a player. 90 00:07:10,162 --> 00:07:16,125 Then we'll increment our counter, and once the counter gets to ten, 91 00:07:16,125 --> 00:07:20,787 counter = 10 then we'll just break out of the loop. 92 00:07:26,100 --> 00:07:28,260 Then we'll need to return topTenPlayers. 93 00:07:31,160 --> 00:07:34,630 All right now up in our main method, we 94 00:07:34,630 --> 00:07:38,760 can use this foreach loop to print all the players from our get topTenPlayers method. 95 00:07:40,415 --> 00:07:44,444 var top ten players = get 96 00:07:44,444 --> 00:07:50,370 toptenplayers and pass it all the players 97 00:07:53,340 --> 00:07:57,700 then we'll need to copy/paste here for 98 00:07:57,700 --> 00:08:02,505 each player in top ten players, and we'll right a little more to the console. 99 00:08:05,940 --> 00:08:14,610 Let's right out Name, FirstName and then we'll print out the points. 100 00:08:16,090 --> 00:08:19,691 We'll say PPG, for points per game. 101 00:08:23,456 --> 00:08:27,629 Then the player.PointsPerGame. 102 00:08:29,487 --> 00:08:31,508 All right let's run it, F5. 103 00:08:35,797 --> 00:08:36,550 Uh-oh. 104 00:08:36,550 --> 00:08:38,500 Looks like we've got a bug. 105 00:08:38,500 --> 00:08:42,130 We sorted points per game ascending and not descending, so 106 00:08:42,130 --> 00:08:45,810 we got the players with the least amount of points per game. 107 00:08:45,810 --> 00:08:50,240 Why don't you take a moment after this video to see if you can fix this bug? 108 00:08:50,240 --> 00:08:52,680 And in the next video I'll show you how I would do it.