C#
IEnumerable
string sorting
LINQ
programming tutorial

How to sort an IEnumerablestring

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Sorting an IEnumerable<string> in C# is usually a LINQ problem, not a data-structure problem. The standard answers are OrderBy for ascending order and OrderByDescending for descending order. The details that matter are the comparer you choose, whether case should matter, and whether you want a lazily evaluated sequence or a materialized list.

Sort Alphabetically With OrderBy

The simplest case is alphabetical ascending order.

csharp
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5IEnumerable<string> names = new[] { "pear", "apple", "banana" };
6IEnumerable<string> sorted = names.OrderBy(x => x);
7
8foreach (var name in sorted)
9{
10    Console.WriteLine(name);
11}

This produces:

text
apple
banana
pear

OrderBy does not modify the original sequence. It returns a new ordered sequence.

Sort Descending

If you want reverse alphabetical order, use OrderByDescending.

csharp
IEnumerable<string> sortedDescending = names.OrderByDescending(x => x);

That is the direct mirror of the ascending case.

Use a String Comparer Explicitly

String sorting is not always as simple as “A to Z.” Case sensitivity and culture rules matter.

For case-insensitive ordinal sorting:

csharp
IEnumerable<string> names = new[] { "pear", "Apple", "banana" };

var sorted = names.OrderBy(x => x, StringComparer.OrdinalIgnoreCase);

For culture-aware sorting:

csharp
var sorted = names.OrderBy(x => x, StringComparer.CurrentCulture);

This is one of the most important practical choices. If the strings are identifiers, file keys, or machine-oriented tokens, ordinal comparison is often better. If they are user-facing text, culture-aware comparison may be more appropriate.

Materialize the Result When Needed

LINQ ordering is deferred. That means the sort happens when you enumerate the result, not at the moment you call OrderBy.

If you need a concrete list immediately, materialize it:

csharp
List<string> sortedList = names
    .OrderBy(x => x, StringComparer.OrdinalIgnoreCase)
    .ToList();

This is useful when you need random access, repeated iteration over a stable snapshot, or an API that specifically expects List<string>.

Sorting Existing Collections

If your underlying data is already a List<string>, another option is to sort in place.

csharp
var names = new List<string> { "pear", "apple", "banana" };
names.Sort(StringComparer.OrdinalIgnoreCase);

That is different from LINQ:

  • 'OrderBy returns a new ordered view'
  • 'List<T>.Sort mutates the list itself'

If the variable type is IEnumerable<string>, use LINQ. If you specifically own a mutable list and want in-place sorting, Sort is fine.

Handle Nulls Deliberately

If the sequence may contain null, decide how you want them ordered.

csharp
IEnumerable<string?> names = new string?[] { "pear", null, "apple" };

var sorted = names.OrderBy(x => x ?? string.Empty, StringComparer.OrdinalIgnoreCase);

Do not leave null-handling to chance if the input is not guaranteed clean.

Keep the Intent Clear

A good rule is:

  • use OrderBy(x => x) for simple default ascending order
  • pass an explicit comparer when case or culture matters
  • call ToList() only when you actually need a materialized result

That keeps the code easy to read and avoids the common mistake of treating all string ordering as equivalent.

Common Pitfalls

A common mistake is assuming string sorting is the same regardless of case or culture. It is not. StringComparer choices can change the result.

Another issue is expecting OrderBy to modify the original collection. LINQ methods return a new sequence; they do not reorder the source in place.

Developers also sometimes forget that deferred execution means the sorting work happens later during enumeration. If the underlying data changes, the observed output can change too.

Finally, if you already have a List<string>, choosing between OrderBy(...).ToList() and Sort(...) should be intentional rather than accidental.

Summary

  • Use OrderBy(x => x) to sort an IEnumerable<string> in ascending order.
  • Use OrderByDescending for descending order.
  • Pass an explicit StringComparer when case sensitivity or culture rules matter.
  • Materialize with ToList() only when you need a concrete sorted list.
  • Remember that LINQ ordering returns a new sequence and does not mutate the original source.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.