Already had a certain exercise when we mapped our users in the one of previous videos, but here I want
to talk about certain a little bit different.
As you can see, you can get the questions answered.
The array of numbers,
which actually means we have an array, for example, three, five, one and one was sorted.
And for this, we're using certain function in JavaScript.
So we're simpler, I saw.
And here we must pass a searchable function and we have such conditions when a less than they were written
here minus one.
If a greater than B than one and zero, if they're equal and previously brought here A and B and here
we can write a smaller than B, then we're returning minus one in that the keys were written in one.
Also, it is important to remember that certain returns the result of the certain, which means we can
put it in a variable, for example, a result, but it will also mutate our array that we created.
And actually, it doesn't really make sense here because this has generated a on the fly.
But if people put it in the variable, then it makes sense.
Here we have our array and here we are searching it.
Let's look in the example I'm going to log in here array and result.
And as you can see in browser here, we are getting exactly the same sorted the rate and this is happening
because sort of mutates existing array and we must remember about it.
Also, there is one more important point to remember in compare function, we can return a number which
is bigger than zero and smaller than zero, and it will be exactly the same.
It will sort of be before a sort, a before B, which actually means we can write our code a little
bit differently and not with minus one and one.
We can simply return here a minus B.
Why is that?
Because we have here two numbers, for example, three minus five.
In this case, it will be less than zero and then we will have this condition sort a before B.
Let's check this out.
Time to load in the page.
We're getting one three five as previously, if people right here b minus A.
Then it will be sorted in the direction, as you can see where getting five through one, which actually
means for certain we can sorted by returning minus one and one or something less than zero and bigger
than zero.
Also, it is important to remember that we can use certain on Lewis primitives if you are trying to
sort here and object, it won't do anything because you can't really compare objects, but you can sort
properties, incite objects, and we have a task to sort an array of our books by authors last name.
And here we can use exactly the same search and function.
So we're right in here.
Books, dot sort.
And here we're getting as an argument, book one and book two.
And now we must compare them properly.
So first of all, we must find a last name of the first author and of the second author.
So he'll it's right.
Also, last name one.
And here we are taking our book one dot author.
And here we want to split our name with space and get here.
First argument.
In this case, we will get to the last name of our author.
Now I will copy paste this line, and here we have author last name too.
And here is the book too.
And now after this, we can compare this to strains.
This is why here we can write our last name one smaller than our thought last name all.
Then we want to return minus one.
In the other case, we will return one.
And actually, as you can see here, we must use minus one and one because it's not numbers.
We can't really subtract strains.
And as you can see here in Tulsa, we didn't console.log anything.
Now let's console.log our books because actually, we mutated our rate.
Let's reload the page.
And as you can see, these are our books, but not on the first place.
We have Susan Collins.
As you can see, it is sorted because previously we had control and then measured.
And then John Jolin, which actually means this code is working and this is how you certain array of
objects by some property.