Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the Sort operations using the comparator interface.
Going to create a class named default methods example to.
Want to make this class executable by adding public static void main.
Let me put this in presentation mode.
There you go.
The first one is we all know we have a collection called.
We call this student database dot, get all students.
This is going to give you a list of students, right?
And then.
Let's give it a name.
Let's import this.
Now what I'm going to do, I'm going to just print this value.
Right.
And then.
Take a look at the result.
The result is not in a readable format.
What I'm going to do, I'm going to create a consumer.
And then I'm going to give it a name.
Going to be student consumer.
So this takes an input called student.
And then what it is going to do, this is going to print the value.
The South.
And student.
There you go.
Now what I'm going to do, I'm going to use.
The student list dot for each method.
We take a look at the for each method.
The for each method takes an input call consumer.
Let's give posit consumer.
Gonna have this one as a static so that we can use this in our for each method.
There we go.
I'm going to run this.
If you take a look at the result, the result is iterated using the for each method one by one.
And we have printed the data.
Right now, the use cases, I want to sort the students by name.
How do I do it?
If you take a look at it, these are not sorted by name because we have Adam here and then Jenny and
then Emily and then Dave.
Dave should be at the second position, right?
How can we achieve that?
We are going to explore different sorting options of different properties using the comparator interface.
Now let's create a method called public void.
Sort by name.
And then what I'm going to do, this method is going to get the list of student as a input.
Student list.
Right.
I'm going to comment this for now.
Now what I'm going to do, I'm going to call.
Let's mark this method as a static method.
We'll call the Sortbyname method and parse the student list.
What are we going to do?
We are going to perform the sort by name.
The first is we will create a comparator.
Comparator.
The comparator is going to be of type student.
Let's give it a name called name Comparator.
Equal to.
Comparator.
Call the comparator method dot as soon as you press dot.
You have different options we are going to do.
We're going to use the comparing function.
And then what is that property you're going to use?
You're going to use Student colon.
Colon name.
Great.
So name is one which we are going to use in order to sort the result.
Correct.
Now what we will do, we will call the student list dot.
The handy sort method and pass the name comparator.
Now what we will do, we will have a logger here.
Called after short.
And this one is before salt.
So.
Going to be.
Before Dot.
This is after.
So this is the advantage of reusing it.
Now, what I'm going to do, I'm going to use the student list dot for each.
And then what I'm going to do, I'm going to call the student consumer.
So I don't have to reread the logic for printing the result.
I just use the student consumer, which is declared the class level so that it can be used throughout
the class.
This is one of the advantage with the introduction of Lambda.
You can actually create a functionality and then assign that to a variable and use it across your project.
Now I'm going to run it and then we'll validate the result.
So now the program executed.
Let's go ahead and validate the result.
If you take a look at it, this is the before sort result.
This is a after sort result in the before sort result.
We have Adam, Jane, Emily, Dave, Sophia and James.
But if you take a look at this after sort result, it has Adam, Dave, Emily, James, Jennie and Sophia.
Basically the result is in sorted order.
They are sorted as per the parents name.
Now what we are going to do, let's explore the another option we want to sort by the GPA.
Basically, if you take a look at it.
It is not sorted here by GPA.
So I want the least GPA to be at the top and the highest GPA to be at the bottom.
So 3.5 should be at the top and then 4.0 should be at the bottom.
How do we do it?
For this purpose.
What I'm going to do, I'm going to create another method.
This method is going to be named as sort by GPA.
Now, in the place of name, what I'm going to do, I'm going to use get GPA.
And since let's go ahead and check what is the type of GPA.
GPA is a double, right?
So comparator has a handy method called comparing.
Comparing double.
I'm going to make use of that.
This is going to compare the double values and then give you the result as per your request.
Now let's call this function.
By GPA, which takes the input as student list.
I'm going to come on this one because it will be hard to read in the console if it has many, many values.
After sort.
Okay, I'm going to have this so that we can differentiate between the name and the GPA.
Okay, let's go ahead and run this one.
I'm going to click on this green icon here.
So the program executed.
Let's compare this one with the before start value in the before.
Start Before start.
It starts with 3.6, 3.8, 4.2 and 3.9, 3.5 and 3.9.
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
4.0.
So the result is sorted as per the GPA.
We want to sort by gender.
You can still do it, but what I'm going to do is I'm going to explore another method called then comparing.
Basically that is called comparator chaining.
You can chain multiple comparators.
What I'm going to do is I'm going to.
Chain to different comparator operations and then consolidate the result and print it.
I'm going to give it a name called Public Static void.
Comparator.
Chaining.
And this method is also going to accept the list of student as an input student list.
So let's copy the same setup here.
I'll put this one down.
The code will be clear.
Want to give after competitor training.
So we are going to create two new competitors.
One is a great competitor.
I'm going to use a greater name.
I'm going to reuse this in order to reuse this.
What I'm going to do, I'm going to put at the top.
For the top.
So this is going to be the great comparator.
Rate comparator.
Let's make this static so that it can be used in the static methods below.
The grade right here.
We cannot use getName.
It's going to be get grade, basically the grade level.
Now our use cases.
I'm going to chain both of these.
How do we do it?
So I'm going to use the student list dot chart method.
And then the next step is we are going to use the grade comparator.
So grade is something in the result.
If you take a look at it, we have the grade value as 423.
Entry.
So basically we have three different grades two, three and four.
First thing is sort by grade.
And it has a method called then comparing in the then comparing what I'm going to do.
I'm going to pass the name comparator.
So grade in each grade, sort the result by their name.
That's what we are trying to achieve here.
And then what I'm going to do, I'm going to call.
The.
For each and print the value.
I'm gonna come on this one and then call comparator chaining.
Pass the student list.
Now, let's go ahead and run this one.
If I run this, the program got executed.
Let's go ahead and validate the result.
If you take a look at it, the result is sorted as per the grade first.
So second grade, third grade and fourth grade.
And then if you take a look at the result, the result is sorted in ascending order.
The names are sorted in ascending order.
Let's verify this with the above one.
For the second grade.
No changes here.
Adam and Jennie, because they are already in sorted order.
Let's take a look at the third grade.
Third grade?
Emily is printed first and then Dave.
But if you take a look at the result here, Dave is printed first and then Emily because as per the
alphabetical order, D comes before E and then the next one is in the fourth grade, Sophia comes first
and then James in the unsorted list.
If you take a look at the sorted list, James comes first and then Sophia, because it is sorted as
per the name and then grade also.
So this is a combination of performing two different sorting operations together and then get to the
result that you are looking for.
With this, we came to the end of this tutorial.
Thank you for watching.