In this lecture I'll show you how to sort a list of values using interfaces.
Let's get started.
Let's say you want to sort the list by titles.
How can you do that.
You can use the sort function.
And I can pass in the list to the sort function like this.
However it doesn't work because the list doesn't satisfy the sort interface.
This is an error because the sort function takes a sort interface value.
So in order to sort the list by the sort function we need to satisfy the sort interface.
So let's take a look at the documentation of the sort interface.
So I'm gonna type godocc sort interface. (You can also use go doc)
Here it is.
This is the sort interface that the sort function takes in order to sort the list.
So now I need to implement these methods so that this sort function can sort the list I'm going to copy
them from here.
I'm going to paste them into the list and now I'm going to implement them one by one.
Let's first attach all of these methods to the list
let's start with the land function here.
I just need to return the number of elements.
So I'm going to return the length of the list like this next I need to compare whether an element comes
before another element in the list so that the sort function can sort the list.
So first I'm going to get the first element from the list and then I'm going to compare the next element's
title with this one so if the less method returns true than the sort function is gonna call the swap
method.
This one here.
So let's implement the swap method as well here.
I'm going to get the first element and the next element and then I'm going to swap their positions on
the list.
That's it.
Now the list satisfies the sort interface.
And I'm going to remove these comments from here.
Okay.
It looks better now.
Let's go to my terminal and run this program as you can see now the list is being sorted.
If you look at these values here you can see that they are not sorted in dictionary order but now they
are being sorted by the sort function in dictionary order.
OK.
Let me explain how it works.
There are three elements on the list and there were two methods.
String and discount then I satisfied the sort interface by adding these methods the sort function takes
a value that satisfies the sort interface.
That's why I can pass the list to the sort function to the short function the list appears as a sort
interface value.
It doesn't know anything else about the list but it does know about these methods.
So it asks the list how many elements do you have.
There are three elements on the list.
So the list returns three then it asks Does Moby Dick come before.
Odyssey the list says go ahead and sort these elements they need to be sorted but the sorter cannot
do that on its own.
It asks the list to swap the elements by itself.
This will go on until the list is fully sorted.
In summary by satisfying the sort interface you can sort any type of collection.
All right let me show you one more example.
Let me run this program.
Now let's say I want to sort this list in reverse order.
How can I do that.
Well if I go to my list and change these index positions you can see that now it sorts in reverse order.
But how can I do that without changing the code here.
Well fortunately there is a function to do that.
It's called sort reverse.
So if I go back to my terminal and run this code again you can see that it sorts the products in reverse
order.
So how does this reverse function really work under the hood.
Let's take a look.
This is the source code of the reverse function as you can see here.
It takes a sort interface value which is the list value here because I pass the list value to the reverse
function and then it puts that list value into a struct called reverse and then returns the reverse
struct.
If I
take a look at the reverse struct here you can see that it anonymously embeds a sort interface value.
This means that it has all of these methods of the list automatically.
But here rather than changing all of the methods of the list it only adds a less method here.
And then it calls the less method of the list by changing the index positions this way.
It actually does the same thing of changing these index index positions all right now let's take a look
at this.
Using visuals maybe you can better understand the let me show you how the sort and reverse functions
work together the sort function executes the land and swap methods.
As usual the reverse strike automatically forwards them to the inner list.
It's because it anonymously embeds the list.
The real trick happens in the less method the reverse struct has its own less method.
When the sort function executes the less method it actually executes the reverse struct's less method
in there.
The less method swaps the indexes like so then it calls the less method of the inner list.
This is how it can reverse sort the list.
Okay I'll help you better understand it now.
Let me show you one more example so you can better understand how this stuff really works.
Let's say I want to sort the list by release dates.
To do that I can still use the sort function.
Now I'm going to pass it a new function by release date and it's gonna take a list.
So how it's gonna work.
Actually it's gonna take a list and it's gonna return a sort interface value.
So this sort function can sort the list.
OK so let's go to the list and add this function.
It's gonna take a list and it's gonna return a sort interface value here.
I'm going to return a new type and you'll see it in a second.
It's gonna wrap the list.
So let's create this new type here in this new type.
I'm going to anonymously embed the list so that I can be able to access all of these methods in the
list but I'm only gonna change the less method so that I can compare the release dates.
Okay I'll let's attach the less method here by release less.
i j bool okay now I'm going to get the first products release date
and then I'm going to compare it using the before methods.
This method compares to time values.
Okay.
For example here it's going to compare whether this time value comes before these next time value like
this.
If the first time value comes before the second time value here the before method is gonna return true
but it doesn't work because the release date is a timestamp here.
I need to give it a time value to do that.
I'm going to get the time type from the timestamp like this.
So if you remember there is a timestamp field in the product and that timestamp field is embedding a
time value.
That's how I can get it from here.
Okay.
This is a timestamp and this is a time field.
All right.
This is the whole implementation.
That's it.
Now let's go to my terminal and run this program.
Great it sorts the products by their release dates.
I can even sort the products' release dates in reverse order.
For example I can say sort of reverse here and then run the program again as you can see now it sorts
the products' release dates in reverse order.
So how does that really work.
Actually you know it.
But let me repeat it again by release date function returns as sort interface value and the sort of
reverse function expects a sort interface value and returns and other one and then that return value
is being passed to the sort function which also expects as sort interface value this way I can change
all of these methods and functions together to achieve this functionality.
All right that's all for now.
Here's a summary of what we spoke about.