WEBVTT 0 00:00.170 --> 00:06.040 In this lecture I'll show you how to sort a list of values using interfaces. 1 00:06.060 --> 00:06.900 Let's get started. 2 00:08.630 --> 00:12.050 Let's say you want to sort the list by titles. 3 00:12.050 --> 00:13.310 How can you do that. 4 00:13.340 --> 00:15.820 You can use the sort function. 5 00:16.270 --> 00:19.880 And I can pass in the list to the sort function like this. 6 00:19.880 --> 00:25.310 However it doesn't work because the list doesn't satisfy the sort interface. 7 00:25.370 --> 00:30.640 This is an error because the sort function takes a sort interface value. 8 00:30.950 --> 00:37.390 So in order to sort the list by the sort function we need to satisfy the sort interface. 9 00:37.430 --> 00:41.730 So let's take a look at the documentation of the sort interface. 10 00:41.840 --> 00:45.990 So I'm gonna type godocc sort interface. (You can also use go doc) 11 00:46.040 --> 00:46.820 Here it is. 12 00:46.820 --> 00:53.410 This is the sort interface that the sort function takes in order to sort the list. 13 00:53.410 --> 01:00.490 So now I need to implement these methods so that this sort function can sort the list I'm going to copy 14 01:00.490 --> 01:01.720 them from here. 15 01:01.750 --> 01:09.500 I'm going to paste them into the list and now I'm going to implement them one by one. 16 01:09.550 --> 01:13.690 Let's first attach all of these methods to the list 17 01:17.130 --> 01:19.710 let's start with the land function here. 18 01:19.770 --> 01:22.810 I just need to return the number of elements. 19 01:22.980 --> 01:31.410 So I'm going to return the length of the list like this next I need to compare whether an element comes 20 01:31.410 --> 01:36.720 before another element in the list so that the sort function can sort the list. 21 01:36.720 --> 01:44.670 So first I'm going to get the first element from the list and then I'm going to compare the next element's 22 01:44.670 --> 01:54.380 title with this one so if the less method returns true than the sort function is gonna call the swap 23 01:54.380 --> 01:55.040 method. 24 01:55.040 --> 01:56.030 This one here. 25 01:56.030 --> 01:59.630 So let's implement the swap method as well here. 26 01:59.900 --> 02:06.620 I'm going to get the first element and the next element and then I'm going to swap their positions on 27 02:06.620 --> 02:07.660 the list. 28 02:07.730 --> 02:08.530 That's it. 29 02:08.540 --> 02:11.530 Now the list satisfies the sort interface. 30 02:11.780 --> 02:15.000 And I'm going to remove these comments from here. 31 02:15.160 --> 02:15.450 Okay. 32 02:15.500 --> 02:16.750 It looks better now. 33 02:16.760 --> 02:23.640 Let's go to my terminal and run this program as you can see now the list is being sorted. 34 02:23.900 --> 02:31.370 If you look at these values here you can see that they are not sorted in dictionary order but now they 35 02:31.370 --> 02:34.550 are being sorted by the sort function in dictionary order. 36 02:35.440 --> 02:36.030 OK. 37 02:36.110 --> 02:37.980 Let me explain how it works. 38 02:38.030 --> 02:42.360 There are three elements on the list and there were two methods. 39 02:42.440 --> 02:50.990 String and discount then I satisfied the sort interface by adding these methods the sort function takes 40 02:50.990 --> 02:54.520 a value that satisfies the sort interface. 41 02:54.620 --> 03:01.850 That's why I can pass the list to the sort function to the short function the list appears as a sort 42 03:01.880 --> 03:03.590 interface value. 43 03:03.590 --> 03:09.530 It doesn't know anything else about the list but it does know about these methods. 44 03:09.560 --> 03:13.660 So it asks the list how many elements do you have. 45 03:13.670 --> 03:15.480 There are three elements on the list. 46 03:15.530 --> 03:21.430 So the list returns three then it asks Does Moby Dick come before. 47 03:21.560 --> 03:30.300 Odyssey the list says go ahead and sort these elements they need to be sorted but the sorter cannot 48 03:30.300 --> 03:31.980 do that on its own. 49 03:32.220 --> 03:36.470 It asks the list to swap the elements by itself. 50 03:36.480 --> 03:40.490 This will go on until the list is fully sorted. 51 03:40.830 --> 03:46.700 In summary by satisfying the sort interface you can sort any type of collection. 52 03:46.940 --> 03:49.190 All right let me show you one more example. 53 03:49.230 --> 03:51.300 Let me run this program. 54 03:51.300 --> 03:54.820 Now let's say I want to sort this list in reverse order. 55 03:54.840 --> 03:55.810 How can I do that. 56 03:55.830 --> 04:04.240 Well if I go to my list and change these index positions you can see that now it sorts in reverse order. 57 04:04.240 --> 04:07.750 But how can I do that without changing the code here. 58 04:07.750 --> 04:10.360 Well fortunately there is a function to do that. 59 04:10.360 --> 04:12.460 It's called sort reverse. 60 04:12.520 --> 04:18.880 So if I go back to my terminal and run this code again you can see that it sorts the products in reverse 61 04:18.880 --> 04:19.560 order. 62 04:19.870 --> 04:24.100 So how does this reverse function really work under the hood. 63 04:24.370 --> 04:25.630 Let's take a look. 64 04:25.660 --> 04:29.920 This is the source code of the reverse function as you can see here. 65 04:29.980 --> 04:36.970 It takes a sort interface value which is the list value here because I pass the list value to the reverse 66 04:36.970 --> 04:44.350 function and then it puts that list value into a struct called reverse and then returns the reverse 67 04:44.350 --> 04:45.020 struct. 68 04:45.340 --> 04:46.290 If I 69 04:46.360 --> 04:52.160 take a look at the reverse struct here you can see that it anonymously embeds a sort interface value. 70 04:52.180 --> 04:57.560 This means that it has all of these methods of the list automatically. 71 04:57.850 --> 05:06.070 But here rather than changing all of the methods of the list it only adds a less method here. 72 05:06.070 --> 05:13.990 And then it calls the less method of the list by changing the index positions this way. 73 05:13.990 --> 05:23.790 It actually does the same thing of changing these index index positions all right now let's take a look 74 05:23.790 --> 05:24.590 at this. 75 05:24.720 --> 05:32.200 Using visuals maybe you can better understand the let me show you how the sort and reverse functions 76 05:32.200 --> 05:38.140 work together the sort function executes the land and swap methods. 77 05:38.170 --> 05:43.810 As usual the reverse strike automatically forwards them to the inner list. 78 05:43.840 --> 05:47.370 It's because it anonymously embeds the list. 79 05:47.370 --> 05:53.940 The real trick happens in the less method the reverse struct has its own less method. 80 05:54.180 --> 06:01.460 When the sort function executes the less method it actually executes the reverse struct's less method 81 06:02.250 --> 06:03.060 in there. 82 06:03.060 --> 06:11.540 The less method swaps the indexes like so then it calls the less method of the inner list. 83 06:11.550 --> 06:14.520 This is how it can reverse sort the list. 84 06:14.520 --> 06:17.360 Okay I'll help you better understand it now. 85 06:19.710 --> 06:25.280 Let me show you one more example so you can better understand how this stuff really works. 86 06:25.290 --> 06:28.240 Let's say I want to sort the list by release dates. 87 06:28.350 --> 06:31.530 To do that I can still use the sort function. 88 06:31.530 --> 06:37.590 Now I'm going to pass it a new function by release date and it's gonna take a list. 89 06:38.130 --> 06:39.750 So how it's gonna work. 90 06:39.750 --> 06:45.270 Actually it's gonna take a list and it's gonna return a sort interface value. 91 06:45.480 --> 06:48.990 So this sort function can sort the list. 92 06:49.020 --> 06:53.240 OK so let's go to the list and add this function. 93 06:53.280 --> 06:57.490 It's gonna take a list and it's gonna return a sort interface value here. 94 06:57.540 --> 07:00.840 I'm going to return a new type and you'll see it in a second. 95 07:00.840 --> 07:02.340 It's gonna wrap the list. 96 07:02.340 --> 07:08.490 So let's create this new type here in this new type. 97 07:08.560 --> 07:14.530 I'm going to anonymously embed the list so that I can be able to access all of these methods in the 98 07:14.530 --> 07:20.470 list but I'm only gonna change the less method so that I can compare the release dates. 99 07:20.530 --> 07:27.070 Okay I'll let's attach the less method here by release less. 100 07:27.150 --> 07:33.880 i j bool okay now I'm going to get the first products release date 101 07:37.530 --> 07:41.100 and then I'm going to compare it using the before methods. 102 07:41.340 --> 07:45.130 This method compares to time values. 103 07:45.150 --> 07:45.670 Okay. 104 07:45.870 --> 07:55.350 For example here it's going to compare whether this time value comes before these next time value like 105 07:55.350 --> 07:55.670 this. 106 07:55.740 --> 08:03.120 If the first time value comes before the second time value here the before method is gonna return true 107 08:03.660 --> 08:08.020 but it doesn't work because the release date is a timestamp here. 108 08:08.070 --> 08:11.460 I need to give it a time value to do that. 109 08:11.580 --> 08:16.230 I'm going to get the time type from the timestamp like this. 110 08:16.230 --> 08:25.740 So if you remember there is a timestamp field in the product and that timestamp field is embedding a 111 08:25.740 --> 08:26.580 time value. 112 08:27.210 --> 08:29.490 That's how I can get it from here. 113 08:29.520 --> 08:29.940 Okay. 114 08:29.970 --> 08:32.840 This is a timestamp and this is a time field. 115 08:33.030 --> 08:33.660 All right. 116 08:33.660 --> 08:36.060 This is the whole implementation. 117 08:36.060 --> 08:37.450 That's it. 118 08:37.470 --> 08:41.010 Now let's go to my terminal and run this program. 119 08:41.010 --> 08:45.050 Great it sorts the products by their release dates. 120 08:45.090 --> 08:49.920 I can even sort the products' release dates in reverse order. 121 08:50.190 --> 08:58.080 For example I can say sort of reverse here and then run the program again as you can see now it sorts 122 08:58.080 --> 09:00.740 the products' release dates in reverse order. 123 09:00.750 --> 09:02.800 So how does that really work. 124 09:02.820 --> 09:03.650 Actually you know it. 125 09:03.660 --> 09:10.610 But let me repeat it again by release date function returns as sort interface value and the sort of 126 09:10.600 --> 09:17.460 reverse function expects a sort interface value and returns and other one and then that return value 127 09:17.490 --> 09:26.020 is being passed to the sort function which also expects as sort interface value this way I can change 128 09:26.070 --> 09:33.890 all of these methods and functions together to achieve this functionality. 129 09:33.900 --> 09:35.830 All right that's all for now. 130 09:35.880 --> 09:38.730 Here's a summary of what we spoke about.