Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
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.
