WEBVTT

00:00.580 --> 00:01.090
Hi everyone.

00:01.090 --> 00:02.500
Welcome back to this tutorial.

00:02.500 --> 00:07.900
In this tutorial we will explore the sorted function which we touched on the previous tutorial.

00:07.900 --> 00:13.690
So here we can explore a lot of different options that are available with the sorted method.

00:13.720 --> 00:19.060
For the purpose of this tutorial, what I'm going to do, I'm going to create a class called.

00:19.970 --> 00:20.750
Dreams.

00:21.860 --> 00:24.140
Comparator example.

00:24.650 --> 00:27.080
I'm going to make this class executable.

00:27.720 --> 00:29.230
Static void main.

00:29.330 --> 00:31.820
This makes a class to be executable.

00:32.690 --> 00:34.430
Report this in presentation mode.

00:36.460 --> 00:37.090
There you go.

00:37.240 --> 00:38.620
The faster bus.

00:40.220 --> 00:42.410
Go ahead and take a look at the student database.

00:45.040 --> 00:52.600
So student database has all these different students with the names Adam, Jenny, Emily, Dave, Sophia,

00:52.600 --> 00:53.570
and James.

00:53.590 --> 00:54.520
The use cases.

00:54.550 --> 01:00.700
We want to sort these result and then print the students in the order.

01:00.700 --> 01:02.620
In the alphabetical order, actually.

01:02.830 --> 01:04.000
How can we achieve that?

01:04.420 --> 01:06.820
Because each and every student is in different grade.

01:06.820 --> 01:10.240
But our use case doesn't matter which grade they are.

01:10.300 --> 01:17.560
The whole database class, the whole students needs to be printed in the sorted order by their name.

01:19.250 --> 01:27.640
So I'm going to create a method called public static list of children.

01:27.650 --> 01:31.730
So this method is going to return the student in sorted order.

01:32.090 --> 01:34.880
So I'm going to name this one by sort.

01:36.510 --> 01:37.560
You dance?

01:38.920 --> 01:39.460
By name.

01:39.610 --> 01:40.990
That's what it is going to do.

01:42.190 --> 01:45.400
Then for this input.

01:45.400 --> 01:45.840
This.

01:46.270 --> 01:46.810
There we go.

01:47.110 --> 01:48.280
The next step is.

01:49.030 --> 01:56.470
We're going to call the student database dot, get all students, and then call the stream method.

01:56.950 --> 02:00.790
And then what we are going to do is we are going to use the sorted.

02:00.820 --> 02:01.660
So sorted.

02:01.660 --> 02:04.450
If you take a look at it, there are two different variations.

02:04.450 --> 02:08.020
One is a sorted method which doesn't accept any input.

02:08.050 --> 02:12.520
The another one is a sorted method, then it accepts a comparator.

02:12.520 --> 02:13.270
So we are going.

02:13.300 --> 02:16.840
We already explored this sorted method in the previous tutorial.

02:16.840 --> 02:22.420
Now we are going to explore the sorted which takes a comparator as an input.

02:23.220 --> 02:24.180
The started.

02:24.360 --> 02:30.870
The first step is we are going to create an instance for comparator and then here we are calling the

02:30.870 --> 02:32.060
comparing method.

02:32.070 --> 02:38.430
So for the comparing method, if you take a look at it, the input that it that it accepts is a function.

02:38.790 --> 02:40.290
So that's what it accepts.

02:40.290 --> 02:44.580
So I'm going to sort the students by name as part of this method, right?

02:44.610 --> 02:45.900
What we can do is.

02:45.900 --> 02:47.640
Student Colon.

02:47.640 --> 02:48.300
Colon.

02:49.070 --> 02:50.810
And then pass the getName name.

02:51.990 --> 02:54.150
And then we are going to collect.

02:55.770 --> 02:56.910
In all do to list.

02:59.500 --> 02:59.950
Here you go.

03:01.820 --> 03:07.820
So either you can return this directly or you can assign this to a variable and then return it.

03:07.880 --> 03:13.120
So we did that, assigning this result to a variable and returning that as part of the previous tutorial.

03:13.130 --> 03:14.840
Now I'm going to return it directly.

03:16.870 --> 03:19.930
John, This is going to return the result.

03:21.040 --> 03:23.830
Meaning return the list of students in the sorted order.

03:23.860 --> 03:25.330
That's what it is going to do.

03:25.360 --> 03:27.370
I'm going to do a for each.

03:29.770 --> 03:33.940
And I'm going to use the syntax of.

03:34.740 --> 03:35.850
At the reference.

03:37.050 --> 03:41.460
But it is going to do I'm going to give a comment here because we are going to do different types of

03:41.460 --> 03:41.850
sorting.

03:41.850 --> 03:43.530
I want to differentiate the results from this.

03:47.870 --> 03:48.740
Students.

03:51.230 --> 03:53.570
Sorted by name.

03:54.530 --> 03:57.020
That's what is happening as part of this, right?

03:57.320 --> 04:02.620
So here what we did, we got the students and then we are calling the sorted method.

04:02.630 --> 04:07.250
But here we are using the sorted method which accepts a comparator.

04:07.250 --> 04:11.090
So you have to pass a property which you want the result to be sorted.

04:11.090 --> 04:13.940
So we wanted to sort the result by their name.

04:13.940 --> 04:20.420
That's the reason why we are passing this name as part of that function and then we are collecting the

04:20.420 --> 04:21.860
whole result as a.

04:22.770 --> 04:25.050
Let's go ahead and print this and check the result.

04:26.520 --> 04:29.600
So the result is we take a look at it.

04:29.610 --> 04:31.900
It is sorted in the alphabetical order.

04:31.920 --> 04:37.380
Adam, Dave, Emily, James, Jenny and Sophia.

04:37.680 --> 04:37.950
So.

04:39.220 --> 04:39.910
The name.

04:39.910 --> 04:43.670
We sorted the collection by the student name.

04:43.690 --> 04:47.620
The next thing is I want to sort the result by their GPA.

04:48.400 --> 04:49.150
Right.

04:49.150 --> 04:54.070
So I don't want to sort the name, sort the collection by their name.

04:54.070 --> 04:55.570
I want to sort it by.

04:59.790 --> 05:00.960
So how do we do?

05:00.960 --> 05:06.780
It is going to be the same result because it is going to still return the list of students.

05:06.780 --> 05:09.870
But in the place of here, we are going to use get.

05:11.320 --> 05:12.550
That's the only difference.

05:13.090 --> 05:13.630
I'm gonna.

05:15.030 --> 05:18.410
Is the same method, but I'm going to replace it here with.

05:19.280 --> 05:22.730
Chart shouldn't be because the flow is the same.

05:24.270 --> 05:25.940
Now copy this chart.

05:25.950 --> 05:27.900
Students by GPA.

05:27.990 --> 05:29.280
That's what I'm going to do.

05:29.310 --> 05:30.990
If I print the result.

05:31.020 --> 05:34.770
Let's print this and then compare between this list and this list.

05:38.090 --> 05:38.750
Here we go.

05:38.790 --> 05:40.130
They're still got printed.

05:43.580 --> 05:44.270
Where do we have it?

05:44.270 --> 05:46.460
So sort student sorted by GPA.

05:47.190 --> 05:47.980
We take a look at it.

05:47.990 --> 05:51.980
Sophia is the first person in the sorted result.

05:52.010 --> 05:59.090
If you take a look at here, Sophia is a last person person because here it is sorted the grade, meaning

05:59.090 --> 06:07.670
the GPA 11 sorry, the level is 3.5 for Sophia, which is the least and the greatest GPA is GPA is Emily.

06:08.180 --> 06:14.730
The GPA is 4.0, so it is sorted as per the GPA in ascending order.

06:14.750 --> 06:20.690
I want the result to be reversed because I don't want the least great person to be on the top.

06:20.690 --> 06:23.600
I want the highest grade person to be on the top.

06:23.600 --> 06:24.740
How do I do it?

06:26.180 --> 06:28.970
Now there is a handy method for that one too.

06:31.190 --> 06:32.120
Descending.

06:33.310 --> 06:36.250
So here you just have to call.

06:37.250 --> 06:37.520
So.

06:38.560 --> 06:40.980
But reversed the called reversed.

06:40.990 --> 06:45.520
Basically, it is going to reverse the result and return you the collection.

06:48.330 --> 06:50.190
Give descending order.

06:51.370 --> 06:52.960
The place of this.

06:52.960 --> 06:53.950
I'll be calling this.

06:55.220 --> 06:57.290
Okay, let's run this and then check.

06:59.610 --> 07:00.780
With the presentation mode.

07:00.780 --> 07:02.140
The result is really hard to see.

07:02.160 --> 07:05.310
I'm going to move away from the presentation mode and check the result.

07:06.000 --> 07:11.740
If we take a look at it here, we started by name here we started with GPA.

07:11.760 --> 07:14.130
With the GPA is ascending order here.

07:14.130 --> 07:17.910
If you take a look at it, the students sorted by descending order.

07:18.360 --> 07:19.230
Take a look at it.

07:19.260 --> 07:21.240
The top GPA is at the.

07:22.140 --> 07:27.150
A spokesman followed by that it goes down at least is 3.5.

07:27.690 --> 07:34.170
So these are the different ways you can sort the result if your use case is that you want to have the

07:34.170 --> 07:35.820
result in a.

07:37.010 --> 07:37.760
Started.

07:39.420 --> 07:39.750
With this.

07:39.750 --> 07:41.520
We came to the end of this tutorial.

07:41.550 --> 07:42.720
Thank you for watching.
