1
00:00:00,510 --> 00:00:07,020
Already had a certain exercise when we mapped our users in the one of previous videos, but here I want

2
00:00:07,020 --> 00:00:09,210
to talk about certain a little bit different.

3
00:00:09,540 --> 00:00:11,800
As you can see, you can get the questions answered.

4
00:00:11,820 --> 00:00:13,080
The array of numbers,

5
00:00:16,350 --> 00:00:21,030
which actually means we have an array, for example, three, five, one and one was sorted.

6
00:00:21,270 --> 00:00:24,270
And for this, we're using certain function in JavaScript.

7
00:00:24,480 --> 00:00:25,740
So we're simpler, I saw.

8
00:00:26,070 --> 00:00:32,250
And here we must pass a searchable function and we have such conditions when a less than they were written

9
00:00:32,250 --> 00:00:33,210
here minus one.

10
00:00:33,360 --> 00:00:39,810
If a greater than B than one and zero, if they're equal and previously brought here A and B and here

11
00:00:39,810 --> 00:00:45,600
we can write a smaller than B, then we're returning minus one in that the keys were written in one.

12
00:00:46,050 --> 00:00:51,840
Also, it is important to remember that certain returns the result of the certain, which means we can

13
00:00:51,840 --> 00:00:57,870
put it in a variable, for example, a result, but it will also mutate our array that we created.

14
00:00:58,260 --> 00:01:02,940
And actually, it doesn't really make sense here because this has generated a on the fly.

15
00:01:03,180 --> 00:01:06,690
But if people put it in the variable, then it makes sense.

16
00:01:06,990 --> 00:01:09,840
Here we have our array and here we are searching it.

17
00:01:09,960 --> 00:01:14,070
Let's look in the example I'm going to log in here array and result.

18
00:01:14,310 --> 00:01:20,190
And as you can see in browser here, we are getting exactly the same sorted the rate and this is happening

19
00:01:20,190 --> 00:01:24,660
because sort of mutates existing array and we must remember about it.

20
00:01:24,880 --> 00:01:31,050
Also, there is one more important point to remember in compare function, we can return a number which

21
00:01:31,050 --> 00:01:35,310
is bigger than zero and smaller than zero, and it will be exactly the same.

22
00:01:35,310 --> 00:01:41,100
It will sort of be before a sort, a before B, which actually means we can write our code a little

23
00:01:41,100 --> 00:01:44,280
bit differently and not with minus one and one.

24
00:01:44,490 --> 00:01:47,250
We can simply return here a minus B.

25
00:01:47,460 --> 00:01:48,150
Why is that?

26
00:01:48,150 --> 00:01:51,330
Because we have here two numbers, for example, three minus five.

27
00:01:51,540 --> 00:01:57,480
In this case, it will be less than zero and then we will have this condition sort a before B.

28
00:01:57,690 --> 00:01:58,500
Let's check this out.

29
00:01:58,500 --> 00:01:59,640
Time to load in the page.

30
00:01:59,790 --> 00:02:04,890
We're getting one three five as previously, if people right here b minus A.

31
00:02:05,100 --> 00:02:10,950
Then it will be sorted in the direction, as you can see where getting five through one, which actually

32
00:02:10,950 --> 00:02:17,760
means for certain we can sorted by returning minus one and one or something less than zero and bigger

33
00:02:17,760 --> 00:02:18,300
than zero.

34
00:02:18,480 --> 00:02:23,880
Also, it is important to remember that we can use certain on Lewis primitives if you are trying to

35
00:02:23,880 --> 00:02:30,330
sort here and object, it won't do anything because you can't really compare objects, but you can sort

36
00:02:30,330 --> 00:02:36,870
properties, incite objects, and we have a task to sort an array of our books by authors last name.

37
00:02:40,140 --> 00:02:43,240
And here we can use exactly the same search and function.

38
00:02:43,440 --> 00:02:44,430
So we're right in here.

39
00:02:44,460 --> 00:02:45,660
Books, dot sort.

40
00:02:45,840 --> 00:02:49,200
And here we're getting as an argument, book one and book two.

41
00:02:49,380 --> 00:02:51,750
And now we must compare them properly.

42
00:02:51,870 --> 00:02:57,000
So first of all, we must find a last name of the first author and of the second author.

43
00:02:57,210 --> 00:02:58,190
So he'll it's right.

44
00:02:58,340 --> 00:03:01,050
Also, last name one.

45
00:03:01,260 --> 00:03:04,620
And here we are taking our book one dot author.

46
00:03:04,710 --> 00:03:08,820
And here we want to split our name with space and get here.

47
00:03:08,820 --> 00:03:09,630
First argument.

48
00:03:09,960 --> 00:03:12,960
In this case, we will get to the last name of our author.

49
00:03:13,170 --> 00:03:17,070
Now I will copy paste this line, and here we have author last name too.

50
00:03:17,250 --> 00:03:18,720
And here is the book too.

51
00:03:18,960 --> 00:03:21,990
And now after this, we can compare this to strains.

52
00:03:22,230 --> 00:03:28,020
This is why here we can write our last name one smaller than our thought last name all.

53
00:03:28,140 --> 00:03:30,060
Then we want to return minus one.

54
00:03:30,090 --> 00:03:31,920
In the other case, we will return one.

55
00:03:32,130 --> 00:03:37,320
And actually, as you can see here, we must use minus one and one because it's not numbers.

56
00:03:37,470 --> 00:03:39,540
We can't really subtract strains.

57
00:03:39,930 --> 00:03:43,440
And as you can see here in Tulsa, we didn't console.log anything.

58
00:03:43,710 --> 00:03:47,820
Now let's console.log our books because actually, we mutated our rate.

59
00:03:48,240 --> 00:03:49,350
Let's reload the page.

60
00:03:49,500 --> 00:03:52,830
And as you can see, these are our books, but not on the first place.

61
00:03:52,830 --> 00:03:54,180
We have Susan Collins.

62
00:03:54,330 --> 00:03:58,890
As you can see, it is sorted because previously we had control and then measured.

63
00:03:59,280 --> 00:04:05,610
And then John Jolin, which actually means this code is working and this is how you certain array of

64
00:04:05,610 --> 00:04:07,440
objects by some property.
