Welcome back.
In this video we're going to combine so many different concepts.
So we're going to combine the generics concept the delegates concept.
We're going to use classes we're going to use for loops within for loops.
So quite a lot of stuff.
But the most important thing is that we're going to build a sorting mechanism that is flexible.
All right.
So let's get started.
So therefore inside of my namespace I'm going to create a public delegate.
And this delegate will return an int and it will be called comparison of generic t, where it's going
to compare t x to t y.
So this delegate is enabling us to create different methods that comply with this declaration.
And then can be used very flexibly.
We're going to see what that means exactly.
But this int here is going to contain a number basically.
And the number, if it's negative, it's going to say, okay, x is bigger.
And if it's positive then it's going to say y is bigger.
And if it's the same then it's going to be x and y are the same.
So same in whatever context.
Same in name, same in age, same in whatever you want to set up.
So I already said age and name.
So let's create a class here.
So this class will be person okay.
So we're going to have a very simple class of person.
And this will be a property of type age or of name age of type int.
And then I'm going to have another property which will be of type string.
And that will contain the name.
Okay.
Nothing too fancy at this point.
But then we're going to have another class class person sorter.
So what this class should do is it should sort people based on a comparison method that we're passing
to it.
How is that even possible?
Well, let me show you.
So we are going to have a public void sort method that will take a person array that I'm going to call
people, and the comparison algorithm or mechanism.
So comparison of person comparison.
So we're basically saying we're passing our delegate here the comparison delegate.
But this time not generically but we're actually saying that we want to use person.
So the delegate will use the person type.
So now what I'm going to do is I'm going to go through every single person in the array, but I'm going
to use a for loop here.
So int I equals zero and I less than people length plus plus.
But here I'm going to say less than people length minus one.
So because we're comparing we don't need to compare ourselves to ourselves.
Right.
We know who we are or what we are, or we don't need to compare the same item with each cell with itself.
So that's what we're avoiding by using this length minus one.
And by using this for int j equals I plus one okay okay.
So what's going on here.
So we have this for loop here this outer for loop.
And what it does is it starts at zero.
And it just says okay go through all people except for the last one.
And why not the last one?
Because the last one is going to be in the inner for loop.
So what is the inner for loop?
It says, okay, let's go from j whatever I is but one higher.
So in this case zero plus one is one.
So J will be one at the beginning.
And then we're saying okay, let's go through all people inside of the array.
And then what we want to do is here inside of this double for loop.
So there we're going to compare.
So using this comparison up here where we are going to pass people at position I and people at position
J.
So by doing so we are passing two different people and we're comparing them to this zero.
So we're saying is it greater than zero?
Because we know that if it's greater than zero and we need to have another bracket in here.
So like so we know that if this is greater than zero, we know that there is a difference in age and
name in whatever we're comparing, whatever our comparison method will be.
And then we inside of here are going to have a person that we're going to store temporarily and that
will be our I person.
And then we take I and inside of it we put the J person and then we put the temp person into J.
So what is going on here in this if statement.
So in these three lines we are swapping.
So first of all here we are comparing I and J using the provided comparison delegate.
Whatever the comparison delegate will be, we will see that in a bit.
And then we're saying if they are not equal, so to speak, so if one of them is bigger than the other,
then what we're going to do is we're going to swap people I and people J if they are in wrong order.
So we're just making sure that the order is correct.
So this is a swapping mechanism.
This is not uncommon not an uncommon algorithm here to use.
So if you think about it we are creating this temporary variable which is let's say whatever the person.
And let me actually create the person array.
Maybe it will be easier for you to to understand what's up.
So I'm going to create a person array here that I'm going to call people.
And I have three people in there.
So I have three persons in there, I have Alice, I have Bob, and I have Charlie.
Alice is 30, Bob is 25 and Charlie is 35.
Okay.
So I'm putting those three in there.
And if we look at this people array, then we know that at people zero we have Alice okay.
And then at people one we have Bob.
And then add people.
Two we have Charlie.
All right.
So let's just take these and put them in here for understanding purposes.
So I'm just saying I is zero.
So the outer loop is Alice and J is one okay.
Zero plus one is one.
So the inner loop is Bob.
So now whatever the comparison algorithm is, is we're going to, in the first iteration, compare Alice
to Bob.
And the second iteration we're going to compare Alice to Charlie.
And the third iteration we're going to compare Bob to Charlie.
And in case whatever the sorting algorithm is that we're going to use, if it's age or if it's the alphabet.
So if we compare names then it's bye alphabet.
Okay.
So Alice, it starts with an a Bob starts with a B Charlie starts with a C.
So you see the order will pretty much stay the same.
So here we're just comparing.
Is one of them bigger than the other with whatever our comparison algorithm is.
And if that's the case then we're saying okay, let's take that person.
Let's say let's take Alice.
And then at the position of Alice, let's put in Bob.
Okay.
So that's what this line does.
And then in the next line we're saying okay.
But now at the position of Bob where Bob earlier was, we're going to put Alice.
So that's what this swap algorithm does.
And it sorts this way.
So it just sorts the the people around so that in the end they have the right order, whatever the comparison
algorithm is that we're going to enable or build.
So now let's actually use the person sorter that we created to understand this even better, where we
actually have this sorter running.
So new person sorter and I'm going to use the sorter to sort.
And now I need to pass the people.
So the person array which will be the people and I need to pass the comparison method because comparison
is a delegate.
So let's create a comparison method.
And I'm going to create one outside of the static void main I'm going to create a static int compare
by age.
And now we're going to take the person in there and actually person X.
And we're going to compare that to person Y.
And now we can go ahead and return x dot h compared to y dot h.
So we're just comparing the two edges of one person to the other person.
So now we can use this compare by h method right here in the sort method.
So we're passing a method to a method because that's what we defined in the sort method.
We said okay, pass me a method that complies to the declaration of this delegate.
So this delegate right here which says give me any type and any other type of the same type, obviously,
but I will compare those two.
Now this will compare the age.
You can see if it's less than zero.
The instance is less than value.
So the instance which means x dot age is less than whatever y is y dot age.
And if it's zero, then they are equal.
And if it's greater than zero, that means that y is greater than x.
So that's what this compare to does.
Okay.
So now if we were to sort this and run this in order to actually see the results, we need to have a
for each loop here.
So for each person in people we're going to display the person name as well as the age okay.
So person dot name and person dot age.
So before we run this let's use console dot read key to keep our program running.
And then we see it sorted it it said Bob, Alice, Charlie.
That's because Bob is the youngest.
He is 25, Alice is 30 and Charlie is 35.
Now what we can do is we can go further and create another sorting algorithm.
So we're going to say or not, the sorting algorithm doesn't change, but we are comparing not by age
but by name.
And then we're going to say, let's compare two people to each other and we're going to return x name
compared to y name.
Okay.
So this again you see less than zero.
This instance precedes string B zero.
They're the same and greater than zero.
This instance follows string B or string b.
Basically it's going to compare them based on their alphabetic order.
So let's actually put in another person in here to make it a little more obvious.
And this one will be Dennis H.
Well, I'm also 35, but I'm close to be turning 36, so let's make me 36 in this example.
And now let's do another comparison.
So I'm going to sort one more time.
but this time I'm going to sort the people to compare by name and everything else stays the same.
Let's run it again and we will see.
First we compare by age.
So we see we have Bob, we have Alice, we have Charlie, then we have Dennis, and then we compare
by name or we sort by name.
So we have Alice, Bob, Charlie, Dennis.
So a, b, c, d You see okay, so a lot is going on in this code, but it is really valuable.
And I would really recommend that you wrap your head around this, especially this part here regarding
the swapping.
And what if it helps you and if you don't get it like um, let's say you don't get it.
What I would recommend you can do in order to figure it out yourself is to use the break points.
You can go through the code step by step, run every single line of code and compare the value and look
at the values.
Look at everything that the code does in order to really understand what happens at every single point,
and what are the values of each of the items.
Okay, so that's a neat little trick for you and see you in the next video.