Welcome back. We're going to learn something that's extremely
useful and powerful but conceptually a bit tricky.
So, hang in there on this one.
Once you get it, you're going to think, wow!
This is really cool. At least I do.
So, let's say we have a list of numbers like L1 created on line one,
and we want to sort them on some property like their absolute value.
So, from line nine,
we're printing out the absolute value of three,
which is just three.
That the absolute value of minus 119 is 119.
On lines 12 and 13,
we're just going through each of the items in
the list and we're outputting their absolute values.
So, we get one, seven, four.
Instead of minus two,
we get its absolute value which is two and finally three.
Now, suppose we want to sort L1 based on the absolute value,
we can just tell the sorted function to use
absolute value as the property that we want to sort by.
The way we do that is we use this other optional parameter called key.
Here's another optional parameter just like we had reverse in the previous video,
here we've got key and we can specify a value,
we're going to specify absolute and that
tells the sorted function to sort by the absolute value.
Here you can see that instead of having minus two first,
minus two is coming after one because its absolute value is bigger.
We've printed out the sorted version of L2.
We can also do it in reverse order by combining the use of reverse
equals true with key equals absolute, and we've got that here.
Now, that one seems pretty straight forward
until you start to really think about what's going on.
The thing that we are passing as
a value for the key parameter is the value of the variable absolute.
That value is a function.
So, we're passing a function absolute to another function sorted.
I hope your first reaction to that is like it should blow your mind like huh!
We're passing a function to a function.
So, yeah that's really weird but eventually you're going to say,
"Wow! That's really powerful."
So, to make sense of this,
we need to have a little mental model,
a way to think about what's going on inside the sorted function.
We're passing it in this function and what is it doing with that function?
What it's doing is before it starts comparing any of the items to each other,
remember comparisons that's like the pairs of
dancers comparing their numbers to each other.
So, before it does any of those comparisons,
the sorted function uses the function that you pass in
absolute and it uses that to determine numbers to assign to each of those dancers.
That is behind the scenes,
when you call sorted,
sorted is going to call the function that you
provide and it's going to call it once for each of the items in the sequence.
It's going to do that to determine some property of that item like its absolute value,
it's going to write it down on a little post-it note that the item carries around.
Then, the sorted function does all the comparisons between the items
but the comparison is always between the values that are on those post-it notes.
So, for example, how is this working?
We've got this sequence one,
seven, four minus two, and three.
Before we do any of those comparisons between pairs of items,
we're going to run our function absolute on each of the items in
turn and we're going to annotate the item with a little post-it note.
So, we're going to have a post-it note for one that it's absolute value is one,
and for seven that it's absolute value is seven,
for four that it's absolute value is four.
Those are uninteresting but four minus two,
we get that the absolute value is two,
and that's going to change things a bit.
Then we go and do these pairwise comparisons one against seven
and seven against four and seven against minus two and all of that.
There's a whole bunch of those that happened behind the scenes in
the sorted function but whenever it's doing a comparison,
it uses the post-it notes to determine the order.
So, we end up with one and then minus two and then three, four, and seven.
The reason we end up with minus two coming after
one is because we had the comparisons based on their post-it notes.
One had the one but minus two had two.
So, minus two with its post-it note of two ends up coming after one.
So, if you think of that as being the process that's going on,
you invoke the sorted function,
the sorted function calls your function,
like absolute, and it calls it once on each of the items in the sequence.
I'm going to prove to you that that really is what's going on.
The way I'm going to prove that is,
I've modified the absolute function here,
just a little bit,
by having it prints something out.
Just added this thing on line four.
So, every time the absolute function gets invoked,
we're going to print something out.
So, on line 10, we're saying to print something,
then we call the sorted function and then we print something to say,
"Hey, we're done with sorting."
Well, you see that line 10 generates that and line 12's print statement
saying that we're done is there then everything in between is stuff that's
happening because of calls to absolute that happened during the execution of line 11.
So, we pass in the value absolute to the sorted function.
We never actually say invoke the absolute function.
We never do that but inside the execution of the sorted function,
it is calling absolute,
and it's calling it one,
two, three, four, five times,
once for each of the five items in
the list and you can see it's calling it for the value one for the value seven,
four, minus two, and three.
So, that's what's going on behind the scenes when the sorted function is executing,
just so we can get the right numbers onto the post-it notes so that it can
use those for doing all the comparisons that lead to sorting of the items.
So, the thing that you pass in for the key parameter has to be a function.
So, often we'll call it the key function.
It has to be a function,
like absolute is here, it's a function.
It has to be a function that takes one input,
that input is going to be one of the items at a time from the list,
and it has to return some value that's going to go on the post-it note.
Some property of the item or typically a number.
Now, I just want to mention one thing here about passing in a function.
We can either pass a function by name,
as we did in originally with this,
we just say the absolute function or we can pass in a lambda expression.
Remember from a previous lesson that
lambda expressions are expressions that produce anonymous functions.
So, instead of specifying absolute here,
we could specify a lambda expression.
So, this is a lambda expression.
This whole expression evaluates to a function object.
That function takes one input x and returns as
its value whatever this expression returns.
Now, if you write this like this,
it might look a little silly to inexperienced programmer because really
this lambda expression is returning a function that does
exactly the same thing that absolute does.
But, you may still want to do that just if it makes it clearer for you.
This is a conceptually challenging thing,
this idea of passing in a function for the key parameter.
I find a lot of students really they just understand it better this
way where the lambda reminds them that they are producing a function.
When they just see key equals absolute,
it doesn't quite click that absolute is referring to a function object.
So, if this helps you,
you're welcome to do it this way.
There are other places where we'll have other lambda expressions
which make even more sense and we'll see that in a later lesson.
So, to summarize, If you want to sort a sequence based on some property of the items,
call sorted and pass a value for the key parameter.
The value for the key parameter has to be a function,
that function takes one item as input and returns a value to write on the post-it note,
a property of the item.
The value for the key parameter can either be the name of a function,
like absolute, or a lambda expression,
like I've shown on line nine here.
So, play around with this a little, do some exercises,
keep trying it until it makes sense and then you'll be able to
sort anything anytime. See you next time.