{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Search, Sort, and Index Lists\n", "\n", "Watch the full [C# 101 video](https://www.youtube.com/watch?v=NJ5ghiutzfY&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=13) for this module." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Search\n", "\n", "In the last notebook, you learned how to find what item was stored at a specific index. Now, given an item, find out its index.\n", "\n", "> Run the code\n", ">\n", "> What index is Sophia at?\n", ">\n", "> What index is \"Scott\" at?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found Ana at 1\r\n" ] } ], "source": [ "using System;\n", "using System.Collections.Generic;\n", "var names = new List { \"Sophia\", \"Ana\", \"Jayme\", \"Bill\" };\n", "string name = \"Ana\";\n", "var index = names.IndexOf(name);\n", "Console.WriteLine($\"Found {name} at {index}\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What does -1 mean?\n", "\n", "If `IndexOf()` returns -1, then that means it couldn't find the item in the list. In fact, you can make a little if statement that works in not finding the item:\n", "\n", "> Run the code.\n", ">\n", "> Try out a few different names." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Scott not found\r\n" ] } ], "source": [ "var names = new List { \"Sophia\", \"Ana\", \"Jayme\", \"Bill\" };\n", "string name = \"Scott\";\n", "var index = names.IndexOf(name);\n", "if(index == -1){\n", " Console.WriteLine($\"{name} not found\");\n", "} else {\n", " Console.WriteLine($\"Found {names[index]} at {index}\");\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sort\n", "\n", "Until now, you've just been putting in names in a random order. But sometimes it's nice to have a list be sorted. `Sort()` takes a list and organizes it. It looks at the variable types and organizes in the most reasonable way it can see - if it's strings, it sorts alphabetically, if it's numbers it organizes from smallest to largest.\n", "\n", "Note that you don't need to write `sortedList = names.Sort()`, you just have to write `names.Sort()`. `Sort()` changes the list itself and you don't have to save the action to a new object.\n", "\n", "> Run the code!\n", ">\n", "> Feel free to add in any other names to see them get sorted." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pre Sorting:\r\n", "Sophia\r\n", "Ana\r\n", "Jayme\r\n", "Bill\r\n", "\r\n", "Post Sorting:\r\n", "Ana\r\n", "Bill\r\n", "Jayme\r\n", "Sophia\r\n" ] } ], "source": [ "var names = new List { \"Sophia\", \"Ana\", \"Jayme\", \"Bill\" };\n", "Console.WriteLine(\"Pre Sorting:\");\n", "foreach(var name in names )\n", "{\n", " Console.WriteLine(name);\n", "}\n", "\n", "names.Sort();\n", "\n", "Console.WriteLine();\n", "Console.WriteLine(\"Post Sorting:\");\n", "foreach(var name in names )\n", "{\n", " Console.WriteLine(name);\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Playground\n", "\n", "Play around with what you've learned! Here's some starting ideas:\n", "\n", "> Make a list of groceries, then sort them! what is the index that 'Carrots' is at?\n", ">\n", "> Try making a list of numbers! Do they sort in the way you expect?\n", ">\n", "> Print out the lists that you've made." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Playground\r\n" ] } ], "source": [ "Console.WriteLine(\"Playground\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Continue learning\n", "\n", "There are plenty more resources out there to learn!\n", "> [⏩ Next Module - Lists of Other Types](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/12-Lists%20of%20Other%20Types.ipynb)\n", ">\n", "> [⏪ Last Module - Arrays, Lists, and Collections](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/10-Arrays%2C%20Lists%2C%20and%20Collections.ipynb)\n", ">\n", "> [Watch the video](https://www.youtube.com/watch?v=NJ5ghiutzfY&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=13)\n", ">\n", "> [Documentation: Arrays, Lists, and Collections](https://docs.microsoft.com/dotnet/csharp/tour-of-csharp/tutorials/arrays-and-collections?WT.mc_id=Educationalcsharp-c9-scottha)\n", ">\n", "> [Start at the beginning: What is C#?](https://www.youtube.com/watch?v=BM4CHBmAPh4&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Other resources\n", "\n", "Here's some more places to explore:\n", "> [Other 101 Videos](https://dotnet.microsoft.com/learn/videos?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [Microsoft Learn](https://docs.microsoft.com/learn/dotnet/?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [C# Documentation](https://docs.microsoft.com/dotnet/csharp/?WT.mc_id=csharpnotebook-35129-website)" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "file_extension": ".cs", "mimetype": "text/x-csharp", "name": "C#", "pygments_lexer": "csharp", "version": "8.0" } }, "nbformat": 4, "nbformat_minor": 4 }