Welcome back.
So now let's look at one of the most useful things that lists have to offer.
And that is sorting.
So the cool thing about lists is really that they allow you to search and sort.
So we're going to look at sorting first and then we're going to look at searching.
So how can I sort a list.
And therefore I'm going to create a separate list or a separate piece of code.
So let's get rid of our beautiful code that we built so far.
Except for maybe this red key here.
And then I'm going to have a list of ints this time.
And in this list I'm going to add a few items.
So we saw how we can add items into a list.
And therefore we use the curly brackets.
And now what I'm going to do is I'm going to put it into one line because I'm using numbers.
Right.
So I'm just going to have a few numbers in here.
So just like that then we have a list of integer numbers.
And let's give it maybe a better name.
So let's just call it numbers.
And now I can sort that list.
How do I sort the list.
Well let's actually go ahead and just enter the name of our list and then look at what methods we have.
So we have add add range.
We have aggregate parallel and so forth.
Contains is a very interesting one.
But then the one that I want to use is going to be under s.
So here there's this sort method okay.
Sorts the elements in the entire list using the default comparer.
All right.
Let's just do that.
Let's sort that list and then let's print it out okay.
So I'm going to display the sorted numbers now.
So I'm going to use a for each loop.
And I in numbers I'm just going to say Console.readline I.
So now you could have called this one a number for example.
So for int number in numbers that's maybe a little more readable.
And now let's run this code and see the beauty of it.
There we are.
It says 359 1015.
Now let's see how the list looked without sorting it.
So before we sorted it and I'm going to add a CW, I'm going to say unsorted list.
And then I'm going to sort it and I'm going to add another CW and say sorted list.
Okay.
Now let's run this again and you will see the unsorted one had ten, five, 15, three, nine and the
sorted list had three, five, nine, ten, 15.
So that's how you can sort a list.
Very simple, very easy.
Now in the next video we're going to look at how we can search for a specific item inside of our list.
So see you in the next video.