WEBVTT

00:00.470 --> 00:01.010
Hi everyone.

00:01.010 --> 00:02.330
Welcome back to this tutorial.

00:02.330 --> 00:08.450
In this tutorial we will code and explore the Sort operations using the comparator interface.

00:08.990 --> 00:16.850
Going to create a class named default methods example to.

00:17.940 --> 00:21.360
Want to make this class executable by adding public static void main.

00:23.830 --> 00:25.480
Let me put this in presentation mode.

00:25.540 --> 00:26.350
There you go.

00:26.380 --> 00:30.490
The first one is we all know we have a collection called.

00:31.670 --> 00:34.850
We call this student database dot, get all students.

00:35.390 --> 00:38.870
This is going to give you a list of students, right?

00:40.300 --> 00:41.020
And then.

00:42.940 --> 00:43.930
Let's give it a name.

00:46.270 --> 00:47.470
Let's import this.

00:48.350 --> 00:50.810
Now what I'm going to do, I'm going to just print this value.

00:54.780 --> 00:55.290
Right.

00:55.290 --> 00:56.370
And then.

00:58.340 --> 00:59.120
Take a look at the result.

00:59.120 --> 01:01.160
The result is not in a readable format.

01:01.190 --> 01:05.180
What I'm going to do, I'm going to create a consumer.

01:06.760 --> 01:08.950
And then I'm going to give it a name.

01:11.970 --> 01:13.860
Going to be student consumer.

01:14.490 --> 01:16.800
So this takes an input called student.

01:17.250 --> 01:21.300
And then what it is going to do, this is going to print the value.

01:22.720 --> 01:23.230
The South.

01:25.720 --> 01:26.770
And student.

01:30.010 --> 01:30.700
There you go.

01:30.820 --> 01:34.810
Now what I'm going to do, I'm going to use.

01:36.990 --> 01:40.440
The student list dot for each method.

01:41.250 --> 01:42.600
We take a look at the for each method.

01:42.600 --> 01:45.270
The for each method takes an input call consumer.

01:46.110 --> 01:48.180
Let's give posit consumer.

01:49.850 --> 01:54.380
Gonna have this one as a static so that we can use this in our for each method.

01:55.070 --> 01:55.650
There we go.

01:55.670 --> 01:56.840
I'm going to run this.

01:58.560 --> 02:03.690
If you take a look at the result, the result is iterated using the for each method one by one.

02:03.690 --> 02:05.700
And we have printed the data.

02:05.940 --> 02:10.680
Right now, the use cases, I want to sort the students by name.

02:10.710 --> 02:11.320
How do I do it?

02:11.340 --> 02:17.730
If you take a look at it, these are not sorted by name because we have Adam here and then Jenny and

02:17.730 --> 02:19.770
then Emily and then Dave.

02:19.800 --> 02:22.470
Dave should be at the second position, right?

02:23.160 --> 02:24.150
How can we achieve that?

02:24.180 --> 02:32.160
We are going to explore different sorting options of different properties using the comparator interface.

02:32.280 --> 02:37.950
Now let's create a method called public void.

02:39.000 --> 02:41.190
Sort by name.

02:42.970 --> 02:49.150
And then what I'm going to do, this method is going to get the list of student as a input.

02:50.800 --> 02:51.550
Student list.

02:52.210 --> 02:52.930
Right.

02:53.020 --> 02:54.460
I'm going to comment this for now.

02:55.800 --> 02:58.230
Now what I'm going to do, I'm going to call.

02:59.670 --> 03:02.070
Let's mark this method as a static method.

03:03.970 --> 03:07.780
We'll call the Sortbyname method and parse the student list.

03:09.380 --> 03:10.430
What are we going to do?

03:10.460 --> 03:13.730
We are going to perform the sort by name.

03:14.030 --> 03:17.030
The first is we will create a comparator.

03:17.690 --> 03:18.770
Comparator.

03:19.370 --> 03:22.190
The comparator is going to be of type student.

03:22.580 --> 03:26.420
Let's give it a name called name Comparator.

03:29.560 --> 03:30.340
Equal to.

03:31.800 --> 03:32.860
Comparator.

03:33.090 --> 03:36.550
Call the comparator method dot as soon as you press dot.

03:36.570 --> 03:39.270
You have different options we are going to do.

03:39.300 --> 03:42.030
We're going to use the comparing function.

03:42.030 --> 03:44.820
And then what is that property you're going to use?

03:44.850 --> 03:46.980
You're going to use Student colon.

03:46.980 --> 03:48.300
Colon name.

03:49.740 --> 03:50.130
Great.

03:50.130 --> 03:54.360
So name is one which we are going to use in order to sort the result.

03:55.300 --> 03:55.670
Correct.

03:55.810 --> 04:00.130
Now what we will do, we will call the student list dot.

04:01.250 --> 04:05.150
The handy sort method and pass the name comparator.

04:06.020 --> 04:09.530
Now what we will do, we will have a logger here.

04:10.790 --> 04:12.050
Called after short.

04:13.200 --> 04:14.880
And this one is before salt.

04:15.300 --> 04:15.750
So.

04:20.810 --> 04:21.620
Going to be.

04:23.390 --> 04:24.880
Before Dot.

04:25.970 --> 04:26.570
This is after.

04:27.590 --> 04:30.540
So this is the advantage of reusing it.

04:30.560 --> 04:36.470
Now, what I'm going to do, I'm going to use the student list dot for each.

04:37.220 --> 04:40.310
And then what I'm going to do, I'm going to call the student consumer.

04:41.170 --> 04:44.010
So I don't have to reread the logic for printing the result.

04:44.010 --> 04:49.380
I just use the student consumer, which is declared the class level so that it can be used throughout

04:49.380 --> 04:49.830
the class.

04:49.860 --> 04:52.950
This is one of the advantage with the introduction of Lambda.

04:52.950 --> 05:00.360
You can actually create a functionality and then assign that to a variable and use it across your project.

05:00.390 --> 05:03.360
Now I'm going to run it and then we'll validate the result.

05:06.320 --> 05:08.810
So now the program executed.

05:08.810 --> 05:10.640
Let's go ahead and validate the result.

05:10.670 --> 05:13.370
If you take a look at it, this is the before sort result.

05:13.490 --> 05:16.490
This is a after sort result in the before sort result.

05:16.490 --> 05:20.300
We have Adam, Jane, Emily, Dave, Sophia and James.

05:20.300 --> 05:28.250
But if you take a look at this after sort result, it has Adam, Dave, Emily, James, Jennie and Sophia.

05:28.280 --> 05:31.730
Basically the result is in sorted order.

05:31.730 --> 05:34.880
They are sorted as per the parents name.

05:35.420 --> 05:42.860
Now what we are going to do, let's explore the another option we want to sort by the GPA.

05:43.460 --> 05:44.960
Basically, if you take a look at it.

05:46.010 --> 05:47.720
It is not sorted here by GPA.

05:47.720 --> 05:54.200
So I want the least GPA to be at the top and the highest GPA to be at the bottom.

05:54.200 --> 05:58.580
So 3.5 should be at the top and then 4.0 should be at the bottom.

05:58.580 --> 05:59.510
How do we do it?

06:00.330 --> 06:01.050
For this purpose.

06:01.050 --> 06:05.250
What I'm going to do, I'm going to create another method.

06:05.280 --> 06:08.670
This method is going to be named as sort by GPA.

06:09.420 --> 06:16.320
Now, in the place of name, what I'm going to do, I'm going to use get GPA.

06:17.100 --> 06:20.280
And since let's go ahead and check what is the type of GPA.

06:20.310 --> 06:21.810
GPA is a double, right?

06:21.930 --> 06:26.340
So comparator has a handy method called comparing.

06:28.070 --> 06:28.970
Comparing double.

06:28.970 --> 06:30.320
I'm going to make use of that.

06:30.680 --> 06:38.360
This is going to compare the double values and then give you the result as per your request.

06:38.540 --> 06:41.240
Now let's call this function.

06:42.440 --> 06:45.820
By GPA, which takes the input as student list.

06:46.390 --> 06:50.980
I'm going to come on this one because it will be hard to read in the console if it has many, many values.

06:52.190 --> 06:53.300
After sort.

06:54.680 --> 06:59.570
Okay, I'm going to have this so that we can differentiate between the name and the GPA.

07:00.980 --> 07:02.420
Okay, let's go ahead and run this one.

07:02.420 --> 07:04.370
I'm going to click on this green icon here.

07:05.850 --> 07:08.550
So the program executed.

07:08.640 --> 07:12.960
Let's compare this one with the before start value in the before.

07:12.960 --> 07:14.250
Start Before start.

07:14.280 --> 07:19.980
It starts with 3.6, 3.8, 4.2 and 3.9, 3.5 and 3.9.

07:20.010 --> 07:28.950
But if you take a look at the sort by value, the result is sorted as 3.5, 3.6, 3.8, 3.9, 3.9 and

07:28.950 --> 07:29.910
4.0.

07:30.030 --> 07:33.450
So the result is sorted as per the GPA.

07:33.990 --> 07:35.550
We want to sort by gender.

07:35.550 --> 07:42.060
You can still do it, but what I'm going to do is I'm going to explore another method called then comparing.

07:42.060 --> 07:45.030
Basically that is called comparator chaining.

07:45.030 --> 07:47.910
You can chain multiple comparators.

07:47.940 --> 07:50.160
What I'm going to do is I'm going to.

07:51.100 --> 07:57.580
Chain to different comparator operations and then consolidate the result and print it.

07:57.910 --> 08:02.140
I'm going to give it a name called Public Static void.

08:03.170 --> 08:04.310
Comparator.

08:06.340 --> 08:07.030
Chaining.

08:07.960 --> 08:15.310
And this method is also going to accept the list of student as an input student list.

08:17.800 --> 08:19.900
So let's copy the same setup here.

08:20.890 --> 08:22.120
I'll put this one down.

08:23.170 --> 08:24.340
The code will be clear.

08:25.680 --> 08:28.350
Want to give after competitor training.

08:28.380 --> 08:31.230
So we are going to create two new competitors.

08:31.230 --> 08:33.120
One is a great competitor.

08:33.870 --> 08:35.520
I'm going to use a greater name.

08:35.520 --> 08:37.580
I'm going to reuse this in order to reuse this.

08:37.590 --> 08:39.750
What I'm going to do, I'm going to put at the top.

08:42.590 --> 08:43.430
For the top.

08:43.900 --> 08:48.470
So this is going to be the great comparator.

08:49.830 --> 08:51.090
Rate comparator.

08:52.650 --> 08:56.760
Let's make this static so that it can be used in the static methods below.

09:00.300 --> 09:01.470
The grade right here.

09:01.470 --> 09:02.640
We cannot use getName.

09:02.640 --> 09:06.210
It's going to be get grade, basically the grade level.

09:06.900 --> 09:08.280
Now our use cases.

09:08.280 --> 09:09.990
I'm going to chain both of these.

09:09.990 --> 09:11.070
How do we do it?

09:12.060 --> 09:15.750
So I'm going to use the student list dot chart method.

09:16.020 --> 09:20.930
And then the next step is we are going to use the grade comparator.

09:20.940 --> 09:22.650
So grade is something in the result.

09:22.650 --> 09:29.250
If you take a look at it, we have the grade value as 423.

09:30.590 --> 09:31.040
Entry.

09:31.040 --> 09:35.660
So basically we have three different grades two, three and four.

09:36.060 --> 09:38.240
First thing is sort by grade.

09:39.050 --> 09:43.360
And it has a method called then comparing in the then comparing what I'm going to do.

09:43.370 --> 09:45.980
I'm going to pass the name comparator.

09:45.980 --> 09:51.230
So grade in each grade, sort the result by their name.

09:51.230 --> 09:52.910
That's what we are trying to achieve here.

09:52.910 --> 09:55.820
And then what I'm going to do, I'm going to call.

09:57.140 --> 09:57.410
The.

09:59.410 --> 10:01.660
For each and print the value.

10:02.590 --> 10:06.760
I'm gonna come on this one and then call comparator chaining.

10:06.790 --> 10:08.140
Pass the student list.

10:09.070 --> 10:11.140
Now, let's go ahead and run this one.

10:13.900 --> 10:16.120
If I run this, the program got executed.

10:16.120 --> 10:17.770
Let's go ahead and validate the result.

10:18.570 --> 10:22.490
If you take a look at it, the result is sorted as per the grade first.

10:22.500 --> 10:25.410
So second grade, third grade and fourth grade.

10:25.470 --> 10:30.960
And then if you take a look at the result, the result is sorted in ascending order.

10:30.960 --> 10:32.740
The names are sorted in ascending order.

10:32.760 --> 10:34.620
Let's verify this with the above one.

10:35.160 --> 10:36.510
For the second grade.

10:36.540 --> 10:37.720
No changes here.

10:37.740 --> 10:40.370
Adam and Jennie, because they are already in sorted order.

10:40.380 --> 10:42.270
Let's take a look at the third grade.

10:42.300 --> 10:42.870
Third grade?

10:42.900 --> 10:45.050
Emily is printed first and then Dave.

10:45.060 --> 10:50.610
But if you take a look at the result here, Dave is printed first and then Emily because as per the

10:50.610 --> 10:58.650
alphabetical order, D comes before E and then the next one is in the fourth grade, Sophia comes first

10:58.650 --> 11:01.230
and then James in the unsorted list.

11:01.230 --> 11:06.360
If you take a look at the sorted list, James comes first and then Sophia, because it is sorted as

11:06.360 --> 11:09.660
per the name and then grade also.

11:09.660 --> 11:16.680
So this is a combination of performing two different sorting operations together and then get to the

11:16.680 --> 11:18.430
result that you are looking for.

11:18.460 --> 11:20.650
With this, we came to the end of this tutorial.

11:20.650 --> 11:21.850
Thank you for watching.
