C# Program to Sort a List of String Names Using the LINQ OrderBy() Method

Sorting a list of string names is a common task in programming, and the LINQ OrderBy() method in C# provides an elegant and efficient way to accomplish this. In this article, we will explore how to sort string lists using LINQ, including both ascending and descending order sorting.

What is LINQ OrderBy() Method?

The LINQ OrderBy() method is used to sort elements of a sequence in ascending order based on a specified key. For descending order, you use OrderByDescending(). These methods work with any IEnumerable<T> collection and return an IOrderedEnumerable<T>.

Syntax

Following is the syntax for the OrderBy() method

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
   this IEnumerable<TSource> source,
   Func<TSource, TKey> keySelector
)

Following is the syntax for the OrderByDescending() method

public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(
   this IEnumerable<TSource> source,
   Func<TSource, TKey> keySelector
)

Parameters

  • source The sequence to be sorted

  • keySelector A function that extracts the key from each element for comparison

Return Value

Both methods return an IOrderedEnumerable<TSource> whose elements are sorted according to the specified key.

LINQ OrderBy() Process Original List John, Alice Peter, Bob Sorted List Alice, Bob John, Peter OrderBy(n => n) Key Selector: n => n Returns each string for alphabetical comparison

Using OrderBy() for Ascending Sort

Example

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
   static void Main() {
      List<string> names = new List<string>() { "John", "Alice", "Peter", "Bob", "Mary" };
   
      var sortedNames = names.OrderBy(n => n);
   
      Console.WriteLine("Sorted Names (Ascending):");
      foreach (var name in sortedNames) {
         Console.WriteLine(name);
      }
   }
}

The output of the above code is

Sorted Names (Ascending):
Alice
Bob
John
Mary
Peter

Using OrderByDescending() for Descending Sort

Example

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
   static void Main() {
      List<string> names = new List<string>() { "John", "Alice", "Peter", "Bob", "Mary" };
   
      var descendingSortedNames = names.OrderByDescending(n => n);
   
      Console.WriteLine("Sorted Names (Descending):");
      foreach (var name in descendingSortedNames) {
         Console.WriteLine(name);
      }
   }
}

The output of the above code is

Sorted Names (Descending):
Peter
Mary
John
Bob
Alice

Sorting by String Length

You can also sort strings by their length instead of alphabetical order

Example

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
   static void Main() {
      List<string> names = new List<string>() { "Alexander", "Bob", "Catherine", "Ed", "Mary" };
   
      var sortedByLength = names.OrderBy(n => n.Length);
   
      Console.WriteLine("Sorted by Length:");
      foreach (var name in sortedByLength) {
         Console.WriteLine($"{name} (Length: {name.Length})");
      }
   }
}

The output of the above code is

Sorted by Length:
Ed (Length: 2)
Bob (Length: 3)
Mary (Length: 4)
Catherine (Length: 9)
Alexander (Length: 9)

Comparison of Sorting Methods

Method Sort Order Key Selector
OrderBy(n => n) Ascending (A-Z) String value
OrderByDescending(n => n) Descending (Z-A) String value
OrderBy(n => n.Length) Ascending by length String length

Conclusion

The LINQ OrderBy() and OrderByDescending() methods provide a powerful and flexible way to sort string collections in C#. You can sort alphabetically, by length, or any custom criteria using lambda expressions as key selectors, making your code both readable and efficient.

Updated on: 2026-03-17T07:04:36+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements