Input List of Names and Sort Alphabetically

C# Arrays: Input List of Names and Sort Alphabetically (Beginner Guide with User Input)


Introduction of the Lesson

In C# programming, arrays are not limited to storing numbers—they are equally powerful when working with text data such as names or words. When dealing with lists of names, one common requirement is to organize them alphabetically, which improves readability and usability in real-world applications such as student records, contact lists, and registration systems.

This lesson focuses on how to accept a list of names from the user, store them in an array, and sort them alphabetically. You will learn how to combine user input, arrays, loops, and built-in sorting methods in C#. Sorting is a foundational skill in programming and is widely used in data processing, searching, and reporting systems.


Learning Objectives

This lesson is structured using Outcome-Based Education (OBE) principles to ensure a clear progression of learning from basic understanding to practical application.

By the end of this lesson, you should be able to:

  • Understand how string arrays work in C#
  • Learn how to collect and store multiple user inputs in an array
  • Practice sorting data alphabetically using built-in methods
  • Apply sorting techniques in real-world scenarios like managing name lists

These objectives ensure that you not only grasp theoretical concepts but also develop hands-on skills. By applying these techniques, you will be able to manage and organize textual data efficiently in your own programs.


Beginner-Friendly Source Code (with User Input)

Instructions:

  1. Open Visual Studio or any C# IDE
  2. Create a new Console Application
  3. Copy and paste the code below
  4. Run the program and follow the prompts
using System;

namespace NameSortingApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("=== Name Sorting Program ===");

            Console.Write("How many names do you want to enter? ");
            int size = Convert.ToInt32(Console.ReadLine());

            string[] names = new string[size];

            // Input names
            for (int i = 0; i < size; i++)
            {
                Console.Write($"Enter name {i + 1}: ");
                names[i] = Console.ReadLine();
            }

            // Sort names alphabetically
            Array.Sort(names);

            Console.WriteLine("\nSorted Names (Alphabetically):");

            // Display sorted names
            foreach (string name in names)
            {
                Console.WriteLine(name);
            }

            Console.ReadLine();
        }
    }
}

Example Output

=== Name Sorting Program ===
How many names do you want to enter? 5
Enter name 1: Maria
Enter name 2: John
Enter name 3: Alex
Enter name 4: Zoe
Enter name 5: Brian

Sorted Names (Alphabetically):
Alex
Brian
John
Maria
Zoe

Lesson Summary

This lesson demonstrated how to use string arrays in C# to store and organize user input efficiently. By applying the built-in Array.Sort() method, we were able to arrange names in alphabetical order with minimal code. You also practiced handling loops, user input, and output formatting. Sorting is a critical operation in programming, and mastering it allows you to build more advanced applications such as search systems, databases, and record management tools.


Multiple Choice Questions

  1. What is used to store multiple names in this program?
    a. Variable
    b. Array
    c. Loop
    d. Method
  2. What does Array.Sort(names) do?
    a. Deletes names
    b. Reverses names
    c. Sorts names alphabetically
    d. Counts names
  3. Which loop is used to input names?
    a. if statement
    b. switch statement
    c. for loop
    d. try-catch
  4. Why is a foreach loop used in displaying names?
    a. To sort names
    b. To easily iterate through all elements
    c. To input values
    d. To validate input
  5. What improvement can enhance this program?
    a. Removing sorting
    b. Adding input validation
    c. Avoiding arrays
    d. Using only one variable

Exercises, Assessment, and Lab Exam

To fully understand sorting and arrays, it is important to extend this basic program into more complex scenarios. The following activities are designed to help you improve your problem-solving and coding skills.

Exercises

  • Modify the program to sort names in reverse (Z–A) order
  • Convert all names to uppercase before sorting
  • Count how many names start with a specific letter

Assessment

  • Create a program that:
    • Sorts names and displays the first and last name alphabetically
    • Searches for a specific name in the list
  • Explain the difference between Array.Sort() and manual sorting (e.g., bubble sort)

Lab Exam

  • Develop a Student Name Management System:
    • Input at least 10 names
    • Sort alphabetically
    • Search for a student
    • Display names in both ascending and descending order
    • Include input validation and clean formatting

You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.

Hire our team to do the project.

, , , , , , ,

Post navigation