How to Sort a List<T> by a property in the object
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Sorting a List<T> by object properties is a common C# task, but there are several correct approaches depending on your goals. If you want readable code and easy multi-level sorting, LINQ is usually best. If you need in-place sorting with less allocation, List<T>.Sort is faster and more memory efficient. Problems typically come from null handling, inconsistent comparers, and accidental string-based numeric sorting. This guide shows practical patterns for property-based sorting, including ascending and descending order, multiple keys, and dynamic property names.
Choose Between OrderBy and List<T>.Sort
LINQ creates a new ordered sequence and is very expressive:
List<T>.Sort modifies the same list and can reduce allocations:
If the list is large and you do not need the original order preserved, in-place sort is often the better choice.
Build Safe Property Comparers
Custom comparers let you control nulls, case sensitivity, and secondary keys. This is important when data quality is uneven.
For descending sort, reverse the comparison:
For nullable values, avoid direct CompareTo if either side may be null:
These details prevent runtime exceptions and unstable output.
Dynamic Sorting by Property Name
APIs and admin screens often receive sort columns dynamically (for example, "Name" or "Score"). Reflection can support this, but validate input to avoid runtime surprises.
For high-throughput code, expression trees or precompiled delegates perform better than repeated reflection.
Verify Correctness with Tests
Sorting bugs can be subtle, especially when values tie. Write small tests for edge cases.
This protects behavior when comparers are modified later.
Practical Verification Workflow
A reliable way to avoid regressions is to validate the solution in three passes: baseline, controlled change, and repeatability check. First, capture a baseline outcome before you apply fixes. This could be a failing command, a wrong output sample, a stack trace, or a screenshot of current behavior. Second, apply one focused change and rerun exactly the same checks so you can attribute improvements to a specific edit. Third, rerun the checks multiple times or with slightly different inputs to ensure the fix is not accidental or data-specific.
A lightweight template you can adapt for most projects looks like this:
If your environment involves tests, add at least one focused regression test that would fail before the fix and pass after it. This turns a one-time troubleshooting success into a durable maintenance improvement, which is especially important when teams rotate ownership or upgrade dependencies later.
Common Pitfalls
- Sorting numeric values stored as strings, which produces lexical order (
"100"before"20"). - Forgetting a secondary key, causing unstable order among equal primary values.
- Using culture-sensitive string comparison unintentionally in backend logic.
- Calling
OrderByand expecting the original list to mutate in place. - Accepting arbitrary property names without validation, leading to runtime errors.
Summary
To sort List<T> by property, use LINQ for clarity and List<T>.Sort for in-place performance. Define comparers that handle nulls and ties explicitly, and test edge cases to guarantee deterministic order. If you support dynamic property names, validate them early and prefer precompiled accessors in performance-critical paths.
Related reading
- How to sort a list/tuple of lists/tuples by the element at a given index
- How to sort a m x n matrix which has all its m rows sorted and n columns sorted?
- How to sort a pandas dataFrame by two or more columns?
- How to sort a stack using only stack operations?
- How to sort a string list consists of digits and alphabets in Java?
- How to sort an array of custom objects by property value?
- How to sort an array of integers?
- How to sort an array of integers faster than quicksort?

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 courseTrack 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.