C#
Sort
OrderBy
LINQ
Programming

C Sort and OrderBy comparison

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

List<T>.Sort and LINQ OrderBy both produce ordered results in C#, but they serve different design goals. Sort mutates an existing list in place, while OrderBy creates an ordered sequence without changing the original source.

That difference affects performance, side effects, and readability. If you pick one by habit instead of by intent, you can end up with extra allocations or with mutations that surprise other code paths.

Sort Mutates, OrderBy Projects

List<T>.Sort changes the list you already have:

csharp
1using System;
2using System.Collections.Generic;
3
4var list = new List<int> { 4, 1, 3, 2 };
5list.Sort();
6
7Console.WriteLine(string.Join(",", list));

OrderBy leaves the source sequence alone and returns an ordered IEnumerable<T>:

csharp
1using System;
2using System.Linq;
3
4var source = new[] { 4, 1, 3, 2 };
5var ordered = source.OrderBy(x => x);
6
7Console.WriteLine("source: " + string.Join(",", source));
8Console.WriteLine("ordered: " + string.Join(",", ordered));

So the first question is simple: do you want to mutate the existing list or produce ordered output as part of a query pipeline?

Understand Deferred Execution

OrderBy is deferred until you enumerate it:

csharp
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5var data = new List<int> { 3, 2, 1 };
6var query = data.OrderBy(x => x);
7
8data.Add(0);
9
10Console.WriteLine(string.Join(",", query));

That output includes 0 because the sorting work happens at enumeration time, not when OrderBy is first called.

If you need a stable snapshot, materialize immediately:

csharp
var snapshot = data.OrderBy(x => x).ToList();

This distinction matters in asynchronous flows, long methods, and shared mutable data.

Compare Performance and Allocation Behavior

For a list you own and intend to mutate, Sort is often the most efficient choice because it works in place and avoids creating a new ordered sequence.

For query composition, OrderBy is usually more readable because it fits naturally with Where, Select, and ThenBy:

csharp
1var result = people
2    .Where(p => p.IsActive)
3    .OrderBy(p => p.LastName)
4    .ThenBy(p => p.FirstName)
5    .ToList();

This is often clearer than manually sorting a temporary list, especially when the code is already written in LINQ style.

Compare Custom Ordering Styles

Sort is comparer-oriented:

csharp
1people.Sort((a, b) =>
2{
3    int ageCmp = a.Age.CompareTo(b.Age);
4    if (ageCmp != 0) return ageCmp;
5    return string.CompareOrdinal(a.Name, b.Name);
6});

OrderBy is key-selector-oriented:

csharp
1var orderedPeople = people
2    .OrderBy(p => p.Age)
3    .ThenBy(p => p.Name)
4    .ToList();

Both are valid. The better one depends on whether your codebase leans toward in-place mutable operations or toward composable query pipelines.

Stability and Intent Matter

Another subtle difference is stability. LINQ ordering is stable across chained operations. List<T>.Sort does not guarantee stable ordering as part of its public contract.

If equal-key items must preserve their original relative order, that can influence the choice. Even when it does not, documenting intent through the chosen API helps other developers understand whether mutation was deliberate.

Common Pitfalls

The biggest mistake is using Sort on a list that other code still expects in its original order. In-place mutation can create subtle bugs if the list is shared.

Another common problem is forgetting that OrderBy is deferred. If the source changes before enumeration, the result changes too.

People also call OrderBy(...).ToList() repeatedly inside loops and create avoidable allocations. If ordering is needed many times, think about where materialization should happen.

Finally, long inline comparer lambdas can become harder to read than a simple OrderBy chain. Readability is a legitimate part of the decision.

Summary

  • 'Sort mutates a List<T> in place.'
  • 'OrderBy produces a new ordered sequence without changing the source.'
  • 'OrderBy is deferred until enumeration, while Sort executes immediately.'
  • Prefer Sort for owned mutable lists and OrderBy for LINQ-style query composition.
  • Choose based on mutation intent, performance, and readability, not just habit.

Course illustration
Course illustration

All Rights Reserved.