>> So we just covered lists of strings in C Sharp.
Now, we're going to sort,
search and index these lists.
>> Let me switch back over here.
I've got my tutorial on the left and Visual Studio on the right,
and when we last left our heroes,
we were saying names at zero,
and we noted when we ran that,
that who was our weird friend,
that weird friend is the zeroth item.
So they call that the index of that.
>> Right.
>> So that's weird.
>> So let's dive in a little bit more into indexes.
It looks like our docs is saying there's
another way to access an index.
>> Yeah. So we could go and say var
and grab and index and say names.indexOf.
So I can actually ask the array or the list,
the collection, "Hey, where is Kendra?
What is the index of Kendra?"
So it's actually going to search for you in that list,
and then I'll go and console.WriteLine index.
Maybe I could even go like this and I could say,
Found, well, I guess I'll just
say Kendra because I've hard coded you here at index.
Okay. Always a little syntax here and there.
There we go. Let's try that. Sound like a plan?
>> Yeah.
>> We'll get rid of our weird friend there.
"Control F5" or hit the "Play" button.
Found Kendra at one.
So zero, one, two, three.
There's Bill. Just to confirm,
found Bill, I expect two.
>> Right
>> No.
>> Zero, one, two, three.
>> Zero, one, two,
three. There we go.
Forgot about Maria there.
Yeah. That's my bad. Now, what
if we search for someone that doesn't exist?
>> I don't know.
>> Let's search for Scott who we have previously removed, right?
>> Yeah.
>> So index of Scott.
>> Negative one. Interesting.
>> It says found because I wrote the word found,
but it didn't actually find it,
right? So that's interesting.
Now, we can combine that as we see
in the sample here with an if statement.
If index is not found else.
So why don't we try that? That's interesting.
I'm going to copy that code in here,
and I'll make it a little bit bigger.
Let's put it there.
So if index is negative one,
when an item is not found,
index returns index, else the name and this is interesting. Look.
>> Is that index?
>> Names at index,
which is going to take the index that we just grabbed.
So this is the number of where Scott is or not,
and then look it up and then print that out.
So what do we think is going to happen,
is Scott in there or not?
When an item is not found,
indexOf returns negative one.
If we go in here and we put it in Kendra, let me do it again.
So let's look at that. The name
names at, that's how I think of that.
So we saw that cup of tea with the less thans and greater thans,
and then when you're doing a list or an array,
it's at, which is the square brackets. Make sense?
>> Definitely. So now,
we know how to find the index of where something is in an array.
That's the type of searching.
>> Right. Let's sort because
we've been just putting these in whatever order, right?
We'll put me back in.
So I'm not going to remove me right now.
>> Good. I wanted you to be included.
>> I appreciate that. I'm going to go and
have right now our original list.
We'll change weird to new friend.
We've got new friend, Scott, Kendra,
and then we are going to manually or
programmatically add Maria and Bill.
>> Sounds good.
>> Then we're going to spin through our list,
just to confirm what that looks like.
>> New friend, Scott, Kendra, Maria, and Bill.
>> Okay. Cool. Looks good.
Now, there it's telling us sort the array.
This is an interesting thing
because I would have thought that I would
have to say names equals names.sort.
>> Yeah.
>> Right. Because I did
replaces before and I did different things,
but in face, sort does it in place,
it sorts it on the spot.
It doesn't make a copy of it which might not be
intuitive if you haven't seen that before.
So let's see what we get when we sort our people.
>> Cool.
It just knew to sort it alphabetically?
How did we know that?
>> Right. That's a great question.
So if we hover over sort,
we can ask that question.
We're using the default comparer.
>> The default comparer. Of course.
>> Of course. Well, so
it turns out that this is a list of strings,
but it could be a list of t,
it could be a list of anything.
So when you compare strings,
like is this string less
than this string, you actually have to do that.
You can say is the word Bill less than the word Kendra?
>> Right.
>> Because Bill comes before Kendra in the alphabet,
Bill is less than,
it's less than in air quotes.
So that's the comparer and that's how they sort.
>> I think even without air quotes because
each character in a string does have a numerical value.
>> That's true.
>> So like a is a very low number,
so even if the string was converted to a number,
sorting them would still
have smallest at the top, biggest at the bottom.
>> That's a really great point. Remember we talked about strings,
strings is a friendship necklace.
>> Right.
>> It's actually a friendship bracelet or necklace
of characters or what they call chars,
and those chars of numbers.
>> Yeah.
>> Yeah. You could sort
the characters within a string
and that would be something as well.
That's a really great point. I didn't think about that.
So when we go and do something like this,
we can look at a larger example.
Here's a bigger example that you could mess with.
Actually, let's do this.
Let's just copy that whole example.
Yeah.
>> I'm just going to go and "Control A"
for all and "Control V" for paste.
Let's just zoom out a little bit and a whole big example here.
Let's run that. So we can
see in the tutorial all the different things
that they've done where we've removed names,
added names, sorted names,
seen them move around.
This is a great little example of
all the different things you can do with lists of stuff.
But in this case, we've only talked about strings.
So let's take a break and we'll come
back and we'll add other types,
other stuff of T to our lists.
>> Sounds great.