Hi everyone.
Welcome back to this tutorial.
In this tutorial we will explore the sorted function which we touched on the previous tutorial.
So here we can explore a lot of different options that are available with the sorted method.
For the purpose of this tutorial, what I'm going to do, I'm going to create a class called.
Dreams.
Comparator example.
I'm going to make this class executable.
Static void main.
This makes a class to be executable.
Report this in presentation mode.
There you go.
The faster bus.
Go ahead and take a look at the student database.
So student database has all these different students with the names Adam, Jenny, Emily, Dave, Sophia,
and James.
The use cases.
We want to sort these result and then print the students in the order.
In the alphabetical order, actually.
How can we achieve that?
Because each and every student is in different grade.
But our use case doesn't matter which grade they are.
The whole database class, the whole students needs to be printed in the sorted order by their name.
So I'm going to create a method called public static list of children.
So this method is going to return the student in sorted order.
So I'm going to name this one by sort.
You dance?
By name.
That's what it is going to do.
Then for this input.
This.
There we go.
The next step is.
We're going to call the student database dot, get all students, and then call the stream method.
And then what we are going to do is we are going to use the sorted.
So sorted.
If you take a look at it, there are two different variations.
One is a sorted method which doesn't accept any input.
The another one is a sorted method, then it accepts a comparator.
So we are going.
We already explored this sorted method in the previous tutorial.
Now we are going to explore the sorted which takes a comparator as an input.
The started.
The first step is we are going to create an instance for comparator and then here we are calling the
comparing method.
So for the comparing method, if you take a look at it, the input that it that it accepts is a function.
So that's what it accepts.
So I'm going to sort the students by name as part of this method, right?
What we can do is.
Student Colon.
Colon.
And then pass the getName name.
And then we are going to collect.
In all do to list.
Here you go.
So either you can return this directly or you can assign this to a variable and then return it.
So we did that, assigning this result to a variable and returning that as part of the previous tutorial.
Now I'm going to return it directly.
John, This is going to return the result.
Meaning return the list of students in the sorted order.
That's what it is going to do.
I'm going to do a for each.
And I'm going to use the syntax of.
At the reference.
But it is going to do I'm going to give a comment here because we are going to do different types of
sorting.
I want to differentiate the results from this.
Students.
Sorted by name.
That's what is happening as part of this, right?
So here what we did, we got the students and then we are calling the sorted method.
But here we are using the sorted method which accepts a comparator.
So you have to pass a property which you want the result to be sorted.
So we wanted to sort the result by their name.
That's the reason why we are passing this name as part of that function and then we are collecting the
whole result as a.
Let's go ahead and print this and check the result.
So the result is we take a look at it.
It is sorted in the alphabetical order.
Adam, Dave, Emily, James, Jenny and Sophia.
So.
The name.
We sorted the collection by the student name.
The next thing is I want to sort the result by their GPA.
Right.
So I don't want to sort the name, sort the collection by their name.
I want to sort it by.
So how do we do?
It is going to be the same result because it is going to still return the list of students.
But in the place of here, we are going to use get.
That's the only difference.
I'm gonna.
Is the same method, but I'm going to replace it here with.
Chart shouldn't be because the flow is the same.
Now copy this chart.
Students by GPA.
That's what I'm going to do.
If I print the result.
Let's print this and then compare between this list and this list.
Here we go.
They're still got printed.
Where do we have it?
So sort student sorted by GPA.
We take a look at it.
Sophia is the first person in the sorted result.
If you take a look at here, Sophia is a last person person because here it is sorted the grade, meaning
the GPA 11 sorry, the level is 3.5 for Sophia, which is the least and the greatest GPA is GPA is Emily.
The GPA is 4.0, so it is sorted as per the GPA in ascending order.
I want the result to be reversed because I don't want the least great person to be on the top.
I want the highest grade person to be on the top.
How do I do it?
Now there is a handy method for that one too.
Descending.
So here you just have to call.
So.
But reversed the called reversed.
Basically, it is going to reverse the result and return you the collection.
Give descending order.
The place of this.
I'll be calling this.
Okay, let's run this and then check.
With the presentation mode.
The result is really hard to see.
I'm going to move away from the presentation mode and check the result.
If we take a look at it here, we started by name here we started with GPA.
With the GPA is ascending order here.
If you take a look at it, the students sorted by descending order.
Take a look at it.
The top GPA is at the.
A spokesman followed by that it goes down at least is 3.5.
So these are the different ways you can sort the result if your use case is that you want to have the
result in a.
Started.
With this.
We came to the end of this tutorial.
Thank you for watching.