Welcome back.
And this video we are going to sort students and we will also check out to find out all the students
of a specific university.
So let's go ahead and create a method.
I am inside of my university manager class and I'm going to go ahead and create a method here called
Sort Students by H.
And first, I create a new variable called Sorted Students, which will be from student in students,
and I'm going to order it by order by student dot age and I'm going to select the student.
So as you can see, we're using order by.
Which is an operator which will sort the output by the given value.
So it will sort the output by our age value.
Also, instead of assigning a new variable from Type II enumerable, we simply use var C as a shortcut,
which works, but it generally makes the code a bit slower because it needs to understand of which type
it should be.
Because var is a very generic type, as you can see, represents a sorted sequence and it already knows
that the t element is student here.
So well it's pretty smart.
It does that while we are working with it right here.
All right.
So now let's print our sorted students on this console, students sorted by H and then let's just print
out all the students.
So A for each loop here and the variable type should be student, the name should be student and the
collection I want to check is sorted.
Students And then here I'm just going to use student dot print.
So I'm going to print out the info about the student for every single student in our sorted students
list, which is created based on our link statement that we have here.
So we are using this operator here and as you know, there are multiple different operators in link.
In the documentation, you'll generally find the standard query operators.
So here we see your order.
By then you'll find different ones here.
Grouping data generation operations, equality operations.
So you can check out all the different operations that there are.
All right.
So I highly recommend to check out the documentation.
All right.
So now let's check it out.
Let's use our university manager to sort students by their H and print them onto the console.
So let's run it.
And then we are students sorted by age.
Student Carla with ID one gender.
Female Age 17.
Then we have Layla, who's 83 and is 19 and so forth.
So you can see it's ordering them in a specific way, going from low to high.
All right.
Now, that's how you can sort students by age.
And now let's look at something that we have done in a similar way before, but we use the databases
for that.
And now we're going to use a link which well can do the same thing with lists.
So let's create a new method.
Public void all students from Bay Jane Tech.
So this will give us all the students which are at the Beijing Technology University.
And now the problem here is that we don't have a list which contains both.
So we need to go by this foreign key university ID and then check the name of the university.
So it's a little more complex.
It's like joining two tables together and you've seen how to do that in the last chapter where we had
to join two tables, the animals table and the zoo table, in order to create this a zoo animals table.
And that's a similar thing that we need to do here.
So let's go ahead and bring them together.
It's going to be an i enumerable of students that i want to have here and I'm going to call it BGT students.
So Beijing technology students.
And here I also use this for each loop from student and students.
Now I need to join university in and in this case it's actually a lowercase u university in universities
on student dot university ID and now we need to equals keyword e equals university thought ID where
university name is equal to Beijing
Tech.
And now select that student.
So select student.
So we're using the join operator, which applies to the University ID of the student.
So we need to check out the University of ID of the student and then we want to see.
Which student has the university ID where the name of that exact university ID is Beijing Tech.
So in our case, we know that it is too.
So the idea of two equals the Beijing Tech University, right?
So now we basically loop through all students and fetch their universities with the joint operator.
Then we simply check if the Associated University has the name Beijing Tech.
And that's it.
That's what we do with this line of code.
Well, it is one line of code.
In our case, it's four lines.
But usually this is just one line of code.
And now we can print that out into the console.
So students from Beijing Tech and now let's go ahead and use a for each loop here, which will be student
in the G t students.
And now let's just print that onto the console.
So print like that.
All right.
Now let's check out all students from Beijing Tech.
Let's use, um, here.
All students from Beijing Tech.
And print it out.
It should be for students.
And we are students from Beijing Tech with the idea of one it's gender.
Male Age 222 from university with the ID of two and so forth.
So you see, we have all of our different students here.
Now, there's one thing that you might have seen that I have when I did that I didn't take care of when
copy and pasting.
Here we have Tony and Frank who have the same ID.
So this one should be three and then we should just go up like that.
One, two, three, four, five, six.
All right, now a little challenge for you.
Create a method which takes the integer value of ID in.
So how is a parameter of type integer?
And it should give us all the students from the ID that was given by the user.
So we need to use console the red line in order to get the input of the user.
Then you need to convert that into an integer.
And then we want to have all the students who are at the uni which has the specific ID.
So as you know, the University of Yale has the ID one and Beijing has the ID too.
So the user, if he enters one, then Carla Anthony should be printed on to the screen.
If she enters too, then all of the Beijing students should be printed on to the screen.
So please go ahead and try that.
All right.
I hope you did.
So I just created a new method, which I call public void.
All students from that uni.
And here I need an integer called ID.
And what I'm going to do here is I'm simply going to copy all of that code from there.
And I'm only going to change this here to ID and.
Hear from that unit and it's going to be zero like that comma ID.
Now, of course.
It should not be the name but the ID of that university.
And instead of BJD students, I'm just going to call it my students.
So what I did is I checked now for the ID of the University of those students, which is equal to the
ID that was entered by the user, which or which was given to this method.
And now we just need to get the input from the user.
So let's go to our main method here and now let's create a new string called input is going to be console
the red line and now I need that as an integer input as INT, which will convert to an integer.
In this case and in 32 and we'll convert our input.
So now I can go ahead and call that method all students from that uni and it will be the input as int.
And I'm not checking.
I'm not using trick catch here.
Well, actually, we could.
So let's create a try catch.
And only run that code if it works.
So if it doesn't work, let's just write something on a console.
Wrong value.
We could do a lot more here.
We could say, okay, what the problem was and so forth, but I'm going to keep it simple.
So now let's run the code and see if we can do something here.
So I'm going to enter one and as you can see, students from uni one.
So now let's run it again and see if it works with two as well too and we'll see students from that
uni two and we get all the students who are at university too.
All right.
I hope you manage to do that.
Great.
So there is one more thing that I would like to show you.
So I showed you those two things.
I showed you how to sort and how to see how to use the the joint operator.
Now, let's go ahead and see some other ways of sorting, because we've seen this one way, but there
are other ways as well.
So I'm going to comment that code out here up to there.
And I want to show you something.
Let's create an array of ints.
And I'm just going to say it's some ints which have the value of 3012 for three and 12, and I'm going
to create an I enumerable of type int and I want it to be the sorted ints.
And now the cool thing is we can use from I in some ints actually some ints plural ordered by or by
I select high and this one here should be a lower caps int and that will order it from four or actually
starting from three, four, 12, 1230.
Now if I want to reverse it, there is also a neat little way to do that.
So I'm going to use another I enumerable int and this will be reversed ints and here I simply use this
sorted installed reverse method.
So that's something that will simply reverse my sorted int I enumerable.
So it will reverse the order.
Now let's print them on a console.
So for each end I in reversed ints and that will write the value onto my screen.
So let's do that.
30, 12, 12, four and three.
So as you see, that's another way of sorting things.
So you don't need to do all of the stuff that we've done here.
If you want to keep it simple and just want to sort integers, for example, you can use this way and
you can reverse the order easily by using the reverse method.
So I enumerable is pretty powerful, has pretty cool features.
But there is an even other alternative to this.
So we could have instead used an ai enumerable of type int called reversed.
Sorted iNts.
So from I in some ints ordered by or ordered by I and now use it as descending.
So descending select.
I now do the same thing with the for each loop, only that we use it with reverse sorted ints now.
All right, so let's run it again.
And you see 30, 12, 1243 and 30, 12, 12 for three.
So these are two different ways of doing it.
And actually you saw three different ways of sorting and handling those things.
All right.
So this is now descending and otherwise it would have been ascending.
So as you can see, there are multiple ways of finding a solution to a problem that you have.
Great.
So I'd say that's good for this video.
Let's go ahead with the next video.
We we'll check out the final step of this little program here.
So see you there.