1 00:00:00,450 --> 00:00:01,240 ‫Welcome back. 2 00:00:01,260 --> 00:00:08,910 ‫And this video we are going to sort students and we will also check out to find out all the students 3 00:00:08,910 --> 00:00:11,310 ‫of a specific university. 4 00:00:11,520 --> 00:00:13,190 ‫So let's go ahead and create a method. 5 00:00:13,200 --> 00:00:21,060 ‫I am inside of my university manager class and I'm going to go ahead and create a method here called 6 00:00:21,060 --> 00:00:24,180 ‫Sort Students by H. 7 00:00:26,070 --> 00:00:38,010 ‫And first, I create a new variable called Sorted Students, which will be from student in students, 8 00:00:39,480 --> 00:00:49,380 ‫and I'm going to order it by order by student dot age and I'm going to select the student. 9 00:00:51,580 --> 00:00:54,970 ‫So as you can see, we're using order by. 10 00:00:55,880 --> 00:01:00,910 ‫Which is an operator which will sort the output by the given value. 11 00:01:00,920 --> 00:01:05,120 ‫So it will sort the output by our age value. 12 00:01:06,710 --> 00:01:14,540 ‫Also, instead of assigning a new variable from Type II enumerable, we simply use var C as a shortcut, 13 00:01:14,540 --> 00:01:21,230 ‫which works, but it generally makes the code a bit slower because it needs to understand of which type 14 00:01:21,230 --> 00:01:21,830 ‫it should be. 15 00:01:21,830 --> 00:01:28,760 ‫Because var is a very generic type, as you can see, represents a sorted sequence and it already knows 16 00:01:28,760 --> 00:01:31,220 ‫that the t element is student here. 17 00:01:31,220 --> 00:01:33,560 ‫So well it's pretty smart. 18 00:01:33,560 --> 00:01:36,410 ‫It does that while we are working with it right here. 19 00:01:37,070 --> 00:01:37,430 ‫All right. 20 00:01:37,430 --> 00:01:48,350 ‫So now let's print our sorted students on this console, students sorted by H and then let's just print 21 00:01:48,350 --> 00:01:49,490 ‫out all the students. 22 00:01:49,490 --> 00:01:59,570 ‫So A for each loop here and the variable type should be student, the name should be student and the 23 00:01:59,570 --> 00:02:02,120 ‫collection I want to check is sorted. 24 00:02:02,120 --> 00:02:08,030 ‫Students And then here I'm just going to use student dot print. 25 00:02:08,030 --> 00:02:14,540 ‫So I'm going to print out the info about the student for every single student in our sorted students 26 00:02:14,540 --> 00:02:20,330 ‫list, which is created based on our link statement that we have here. 27 00:02:20,960 --> 00:02:28,580 ‫So we are using this operator here and as you know, there are multiple different operators in link. 28 00:02:30,540 --> 00:02:35,120 ‫In the documentation, you'll generally find the standard query operators. 29 00:02:35,130 --> 00:02:37,830 ‫So here we see your order. 30 00:02:37,830 --> 00:02:41,730 ‫By then you'll find different ones here. 31 00:02:42,660 --> 00:02:46,560 ‫Grouping data generation operations, equality operations. 32 00:02:46,560 --> 00:02:49,980 ‫So you can check out all the different operations that there are. 33 00:02:50,700 --> 00:02:51,330 ‫All right. 34 00:02:51,330 --> 00:02:54,750 ‫So I highly recommend to check out the documentation. 35 00:02:54,990 --> 00:02:55,560 ‫All right. 36 00:02:55,560 --> 00:02:57,130 ‫So now let's check it out. 37 00:02:57,150 --> 00:03:05,640 ‫Let's use our university manager to sort students by their H and print them onto the console. 38 00:03:05,670 --> 00:03:06,690 ‫So let's run it. 39 00:03:07,350 --> 00:03:09,690 ‫And then we are students sorted by age. 40 00:03:09,750 --> 00:03:11,790 ‫Student Carla with ID one gender. 41 00:03:11,790 --> 00:03:13,170 ‫Female Age 17. 42 00:03:13,170 --> 00:03:18,180 ‫Then we have Layla, who's 83 and is 19 and so forth. 43 00:03:18,180 --> 00:03:24,600 ‫So you can see it's ordering them in a specific way, going from low to high. 44 00:03:25,230 --> 00:03:25,830 ‫All right. 45 00:03:26,490 --> 00:03:29,790 ‫Now, that's how you can sort students by age. 46 00:03:29,790 --> 00:03:35,160 ‫And now let's look at something that we have done in a similar way before, but we use the databases 47 00:03:35,160 --> 00:03:35,550 ‫for that. 48 00:03:35,550 --> 00:03:41,570 ‫And now we're going to use a link which well can do the same thing with lists. 49 00:03:41,580 --> 00:03:44,790 ‫So let's create a new method. 50 00:03:44,940 --> 00:03:55,980 ‫Public void all students from Bay Jane Tech. 51 00:03:57,120 --> 00:04:03,200 ‫So this will give us all the students which are at the Beijing Technology University. 52 00:04:03,210 --> 00:04:09,240 ‫And now the problem here is that we don't have a list which contains both. 53 00:04:09,240 --> 00:04:17,460 ‫So we need to go by this foreign key university ID and then check the name of the university. 54 00:04:17,640 --> 00:04:19,830 ‫So it's a little more complex. 55 00:04:19,830 --> 00:04:25,770 ‫It's like joining two tables together and you've seen how to do that in the last chapter where we had 56 00:04:25,770 --> 00:04:33,360 ‫to join two tables, the animals table and the zoo table, in order to create this a zoo animals table. 57 00:04:33,360 --> 00:04:35,610 ‫And that's a similar thing that we need to do here. 58 00:04:35,610 --> 00:04:39,960 ‫So let's go ahead and bring them together. 59 00:04:39,960 --> 00:04:48,270 ‫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. 60 00:04:48,270 --> 00:04:52,260 ‫So Beijing technology students. 61 00:04:52,830 --> 00:04:59,160 ‫And here I also use this for each loop from student and students. 62 00:05:00,330 --> 00:05:15,330 ‫Now I need to join university in and in this case it's actually a lowercase u university in universities 63 00:05:16,170 --> 00:05:32,760 ‫on student dot university ID and now we need to equals keyword e equals university thought ID where 64 00:05:32,760 --> 00:05:41,310 ‫university name is equal to Beijing 65 00:05:43,200 --> 00:05:43,740 ‫Tech. 66 00:05:44,970 --> 00:05:46,770 ‫And now select that student. 67 00:05:46,770 --> 00:05:48,690 ‫So select student. 68 00:05:52,290 --> 00:05:58,650 ‫So we're using the join operator, which applies to the University ID of the student. 69 00:05:58,650 --> 00:06:05,580 ‫So we need to check out the University of ID of the student and then we want to see. 70 00:06:06,520 --> 00:06:14,510 ‫Which student has the university ID where the name of that exact university ID is Beijing Tech. 71 00:06:14,530 --> 00:06:18,190 ‫So in our case, we know that it is too. 72 00:06:18,490 --> 00:06:23,680 ‫So the idea of two equals the Beijing Tech University, right? 73 00:06:25,030 --> 00:06:31,370 ‫So now we basically loop through all students and fetch their universities with the joint operator. 74 00:06:31,390 --> 00:06:36,490 ‫Then we simply check if the Associated University has the name Beijing Tech. 75 00:06:37,060 --> 00:06:37,710 ‫And that's it. 76 00:06:37,720 --> 00:06:40,470 ‫That's what we do with this line of code. 77 00:06:40,480 --> 00:06:41,710 ‫Well, it is one line of code. 78 00:06:41,710 --> 00:06:42,970 ‫In our case, it's four lines. 79 00:06:42,970 --> 00:06:45,880 ‫But usually this is just one line of code. 80 00:06:46,060 --> 00:06:49,390 ‫And now we can print that out into the console. 81 00:06:49,390 --> 00:07:01,930 ‫So students from Beijing Tech and now let's go ahead and use a for each loop here, which will be student 82 00:07:01,960 --> 00:07:07,630 ‫in the G t students. 83 00:07:08,050 --> 00:07:11,320 ‫And now let's just print that onto the console. 84 00:07:11,320 --> 00:07:13,180 ‫So print like that. 85 00:07:14,500 --> 00:07:14,840 ‫All right. 86 00:07:14,860 --> 00:07:18,160 ‫Now let's check out all students from Beijing Tech. 87 00:07:19,000 --> 00:07:20,680 ‫Let's use, um, here. 88 00:07:20,980 --> 00:07:23,110 ‫All students from Beijing Tech. 89 00:07:23,110 --> 00:07:24,560 ‫And print it out. 90 00:07:24,580 --> 00:07:26,080 ‫It should be for students. 91 00:07:27,250 --> 00:07:32,470 ‫And we are students from Beijing Tech with the idea of one it's gender. 92 00:07:32,470 --> 00:07:38,950 ‫Male Age 222 from university with the ID of two and so forth. 93 00:07:39,040 --> 00:07:43,600 ‫So you see, we have all of our different students here. 94 00:07:44,140 --> 00:07:50,350 ‫Now, there's one thing that you might have seen that I have when I did that I didn't take care of when 95 00:07:50,350 --> 00:07:51,130 ‫copy and pasting. 96 00:07:51,130 --> 00:07:54,610 ‫Here we have Tony and Frank who have the same ID. 97 00:07:54,640 --> 00:07:59,360 ‫So this one should be three and then we should just go up like that. 98 00:07:59,380 --> 00:08:01,030 ‫One, two, three, four, five, six. 99 00:08:01,420 --> 00:08:03,160 ‫All right, now a little challenge for you. 100 00:08:03,190 --> 00:08:09,340 ‫Create a method which takes the integer value of ID in. 101 00:08:09,340 --> 00:08:12,340 ‫So how is a parameter of type integer? 102 00:08:12,340 --> 00:08:18,610 ‫And it should give us all the students from the ID that was given by the user. 103 00:08:18,610 --> 00:08:23,200 ‫So we need to use console the red line in order to get the input of the user. 104 00:08:23,200 --> 00:08:26,260 ‫Then you need to convert that into an integer. 105 00:08:26,500 --> 00:08:31,960 ‫And then we want to have all the students who are at the uni which has the specific ID. 106 00:08:31,990 --> 00:08:37,570 ‫So as you know, the University of Yale has the ID one and Beijing has the ID too. 107 00:08:37,570 --> 00:08:42,400 ‫So the user, if he enters one, then Carla Anthony should be printed on to the screen. 108 00:08:42,400 --> 00:08:47,470 ‫If she enters too, then all of the Beijing students should be printed on to the screen. 109 00:08:47,860 --> 00:08:49,510 ‫So please go ahead and try that. 110 00:08:51,710 --> 00:08:52,190 ‫All right. 111 00:08:52,190 --> 00:08:53,270 ‫I hope you did. 112 00:08:53,450 --> 00:08:58,620 ‫So I just created a new method, which I call public void. 113 00:08:58,640 --> 00:09:02,330 ‫All students from that uni. 114 00:09:02,510 --> 00:09:06,110 ‫And here I need an integer called ID. 115 00:09:06,440 --> 00:09:11,090 ‫And what I'm going to do here is I'm simply going to copy all of that code from there. 116 00:09:11,090 --> 00:09:20,000 ‫And I'm only going to change this here to ID and. 117 00:09:22,100 --> 00:09:30,860 ‫Hear from that unit and it's going to be zero like that comma ID. 118 00:09:32,580 --> 00:09:33,710 ‫Now, of course. 119 00:09:35,510 --> 00:09:38,930 ‫It should not be the name but the ID of that university. 120 00:09:39,590 --> 00:09:43,970 ‫And instead of BJD students, I'm just going to call it my students. 121 00:09:44,660 --> 00:09:53,750 ‫So what I did is I checked now for the ID of the University of those students, which is equal to the 122 00:09:53,750 --> 00:09:58,610 ‫ID that was entered by the user, which or which was given to this method. 123 00:09:58,610 --> 00:10:01,540 ‫And now we just need to get the input from the user. 124 00:10:01,550 --> 00:10:11,510 ‫So let's go to our main method here and now let's create a new string called input is going to be console 125 00:10:11,510 --> 00:10:23,330 ‫the red line and now I need that as an integer input as INT, which will convert to an integer. 126 00:10:23,570 --> 00:10:27,950 ‫In this case and in 32 and we'll convert our input. 127 00:10:29,810 --> 00:10:38,510 ‫So now I can go ahead and call that method all students from that uni and it will be the input as int. 128 00:10:40,520 --> 00:10:42,170 ‫And I'm not checking. 129 00:10:42,170 --> 00:10:44,270 ‫I'm not using trick catch here. 130 00:10:44,300 --> 00:10:45,560 ‫Well, actually, we could. 131 00:10:45,740 --> 00:10:47,810 ‫So let's create a try catch. 132 00:10:50,300 --> 00:10:53,150 ‫And only run that code if it works. 133 00:10:53,150 --> 00:10:57,260 ‫So if it doesn't work, let's just write something on a console. 134 00:10:58,580 --> 00:11:00,380 ‫Wrong value. 135 00:11:01,880 --> 00:11:03,320 ‫We could do a lot more here. 136 00:11:03,320 --> 00:11:07,220 ‫We could say, okay, what the problem was and so forth, but I'm going to keep it simple. 137 00:11:07,250 --> 00:11:11,750 ‫So now let's run the code and see if we can do something here. 138 00:11:11,750 --> 00:11:16,100 ‫So I'm going to enter one and as you can see, students from uni one. 139 00:11:16,100 --> 00:11:23,090 ‫So now let's run it again and see if it works with two as well too and we'll see students from that 140 00:11:23,090 --> 00:11:26,660 ‫uni two and we get all the students who are at university too. 141 00:11:27,230 --> 00:11:27,710 ‫All right. 142 00:11:27,710 --> 00:11:29,270 ‫I hope you manage to do that. 143 00:11:30,050 --> 00:11:30,530 ‫Great. 144 00:11:30,530 --> 00:11:32,960 ‫So there is one more thing that I would like to show you. 145 00:11:32,960 --> 00:11:34,580 ‫So I showed you those two things. 146 00:11:34,580 --> 00:11:40,370 ‫I showed you how to sort and how to see how to use the the joint operator. 147 00:11:40,460 --> 00:11:46,820 ‫Now, let's go ahead and see some other ways of sorting, because we've seen this one way, but there 148 00:11:46,820 --> 00:11:48,320 ‫are other ways as well. 149 00:11:48,320 --> 00:11:53,990 ‫So I'm going to comment that code out here up to there. 150 00:11:54,350 --> 00:11:57,260 ‫And I want to show you something. 151 00:11:57,410 --> 00:11:59,240 ‫Let's create an array of ints. 152 00:11:59,240 --> 00:12:08,900 ‫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 153 00:12:08,900 --> 00:12:16,730 ‫to create an I enumerable of type int and I want it to be the sorted ints. 154 00:12:17,150 --> 00:12:29,480 ‫And now the cool thing is we can use from I in some ints actually some ints plural ordered by or by 155 00:12:29,930 --> 00:12:40,520 ‫I select high and this one here should be a lower caps int and that will order it from four or actually 156 00:12:40,520 --> 00:12:43,580 ‫starting from three, four, 12, 1230. 157 00:12:43,670 --> 00:12:47,750 ‫Now if I want to reverse it, there is also a neat little way to do that. 158 00:12:47,750 --> 00:12:56,420 ‫So I'm going to use another I enumerable int and this will be reversed ints and here I simply use this 159 00:12:56,420 --> 00:12:59,180 ‫sorted installed reverse method. 160 00:12:59,180 --> 00:13:06,110 ‫So that's something that will simply reverse my sorted int I enumerable. 161 00:13:06,170 --> 00:13:08,360 ‫So it will reverse the order. 162 00:13:08,930 --> 00:13:11,300 ‫Now let's print them on a console. 163 00:13:11,300 --> 00:13:21,920 ‫So for each end I in reversed ints and that will write the value onto my screen. 164 00:13:21,920 --> 00:13:23,150 ‫So let's do that. 165 00:13:28,170 --> 00:13:30,650 ‫30, 12, 12, four and three. 166 00:13:30,660 --> 00:13:33,360 ‫So as you see, that's another way of sorting things. 167 00:13:33,360 --> 00:13:36,810 ‫So you don't need to do all of the stuff that we've done here. 168 00:13:36,810 --> 00:13:42,570 ‫If you want to keep it simple and just want to sort integers, for example, you can use this way and 169 00:13:42,570 --> 00:13:47,520 ‫you can reverse the order easily by using the reverse method. 170 00:13:47,520 --> 00:13:51,060 ‫So I enumerable is pretty powerful, has pretty cool features. 171 00:13:53,090 --> 00:13:56,330 ‫But there is an even other alternative to this. 172 00:13:56,330 --> 00:14:05,240 ‫So we could have instead used an ai enumerable of type int called reversed. 173 00:14:07,530 --> 00:14:09,690 ‫Sorted iNts. 174 00:14:10,830 --> 00:14:21,810 ‫So from I in some ints ordered by or ordered by I and now use it as descending. 175 00:14:21,810 --> 00:14:23,690 ‫So descending select. 176 00:14:23,700 --> 00:14:33,390 ‫I now do the same thing with the for each loop, only that we use it with reverse sorted ints now. 177 00:14:34,430 --> 00:14:36,290 ‫All right, so let's run it again. 178 00:14:38,300 --> 00:14:42,350 ‫And you see 30, 12, 1243 and 30, 12, 12 for three. 179 00:14:42,350 --> 00:14:45,110 ‫So these are two different ways of doing it. 180 00:14:45,110 --> 00:14:49,280 ‫And actually you saw three different ways of sorting and handling those things. 181 00:14:49,760 --> 00:14:50,150 ‫All right. 182 00:14:50,150 --> 00:14:54,440 ‫So this is now descending and otherwise it would have been ascending. 183 00:14:54,440 --> 00:15:00,350 ‫So as you can see, there are multiple ways of finding a solution to a problem that you have. 184 00:15:01,100 --> 00:15:01,550 ‫Great. 185 00:15:01,550 --> 00:15:03,680 ‫So I'd say that's good for this video. 186 00:15:03,680 --> 00:15:05,480 ‫Let's go ahead with the next video. 187 00:15:05,480 --> 00:15:09,020 ‫We we'll check out the final step of this little program here. 188 00:15:09,020 --> 00:15:10,010 ‫So see you there.