How to Sorting List in Alphabetical Order in C#

Haider Ali Feb 02, 2024
  1. Use the Sort() Method to Sort a List in Alphabetical Order in C#
  2. Use the foreach Loop to Print the List Alphabetical in C#
How to Sorting List in Alphabetical Order in C#

This guide shows how to sort words in alphabetical order in c#. There is a built-in function in c# that we can use to sort a list.

Use the Sort() Method to Sort a List in Alphabetical Order in C#

First of all, using System.Collections.Generic;, this is the library you need to import to use the list in c#. We need to use Sort() to sort a list.

After that, we need to use the comparator to compare the two strings. For instance, take a look at the following piece of code.

citizens.Sort((x, y) => string.Compare(x.Name, y.Name));

In the above line of code, citizens is the list, and we compare the citizens’ names to sort the citizen list in alphabetical order.

Use the foreach Loop to Print the List Alphabetical in C#

using System;

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;  // library import to use list;

namespace list_sort {
  class Program {
    static void Main(string[] args) {
      List<Person> citizens = new List<Person>(5);  // Creating List Of Person having size 5;
                                                    // Adding Persons in List
      citizens.Add(new Person("Mark", "Zuker", "Silicon Valley United States", 50));
      citizens.Add(new Person("Bill ", "Gates", "Silicon Valley United States", 70));
      citizens.Add(new Person("Jeff", "Bezoz", "Silicon Valley United States", 40));
      citizens.Add(new Person("Elon", "Musk", "Silicon Valley United States", 20));
      citizens.Add(new Person("Antony", "Gates", "Silicon Valley United States", 30));

      Console.WriteLine(":::::::::::::::::::::::::::::::Before Sorting ::::::::::::::::::::::::");
      // Prinring The List Names...
      foreach (Person p in citizens) {  // Loop through List with foreach
        Console.WriteLine(p.Name);
      }
      Console.WriteLine(":::::::::::::::::::::::::::::::After Sorting :::::::::::::::::");
      // Problem #  Sort List of Citizens According to Citizen Names orderby-alphabetical-order
      citizens.Sort((x, y) => string.Compare(x.Name, y.Name));
      foreach (Person p in citizens)  // Prniting After alphabetical Sort.
      {                               // Loop through List with foreach
        Console.WriteLine(p.Name);
      }
      Console.ReadKey();  // to Stay On Screen.
    }
  }
  class Person {
    public String Name;
    public String LastName;
    public String Address;
    public int age;
    public Person(String Name, String LastName, String Address, int Age) {
      this.Name = Name;
      this.LastName = LastName;
      this.Address = Address;
      this.age = Age;
    }
  }
}

First, we created a list of citizens and then added names, addresses, and ages. We printed it before sorting the list, and then we printed it after sorting the list.

Output:

:::::::::::::::::::::::::::::::Before Sorting ::::::::::::::::::::::::
Mark
Bill
Jeff
Elon
Antony
:::::::::::::::::::::::::::::::After Sorting :::::::::::::::::
Antony
Bill
Elon
Jeff
Mark
Author: Haider Ali
Haider Ali avatar Haider Ali avatar

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.

LinkedIn

Related Article - Csharp List

Related Article - Csharp Sort