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:
OrderBy leaves the source sequence alone and returns an ordered IEnumerable<T>:
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:
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:
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:
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:
OrderBy is key-selector-oriented:
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
- '
Sortmutates aList<T>in place.' - '
OrderByproduces a new ordered sequence without changing the source.' - '
OrderByis deferred until enumeration, whileSortexecutes immediately.' - Prefer
Sortfor owned mutable lists andOrderByfor LINQ-style query composition. - Choose based on mutation intent, performance, and readability, not just habit.

