Question
I have a class named Order with properties such as OrderId, OrderDate, Quantity, and Total. I also have a list of Order objects:
List<Order> objListOrder = new List<Order>();
GetOrderList(objListOrder); // Fill the list of orders
I want to sort this list based on one property of the Order object, such as OrderDate or OrderId.
How can I sort a List<Order> by a property in C#?
Short Answer
By the end of this page, you will understand how to sort a List<T> by one of an object's properties in C#. You will learn the difference between List.Sort() and LINQ methods like OrderBy() and OrderByDescending(), when to use each one, and how to sort by multiple properties safely and clearly.
Concept
Sorting a list of objects means arranging the items in a specific order based on one or more values inside each object.
In this case, each item in the list is an Order object. Since Order has properties like OrderId and OrderDate, you can sort the list using one of those properties as the sorting key.
In C#, there are two very common ways to do this:
List<T>.Sort(...)— sorts the existing list in place- LINQ
OrderBy(...)/OrderByDescending(...)— returns a new sorted sequence
This matters in real programming because object collections are everywhere:
- showing records in a UI
- sorting API results
- ordering reports by date
- processing highest or lowest values first
- grouping and displaying business data in a meaningful way
The key idea is simple: you tell C# which property to compare, and C# uses that property to arrange the objects.
For example:
- sort by
OrderIdfor numeric order - sort by
OrderDatefor chronological order - sort by
Totalto see cheapest or most expensive orders first
If you need only a quick one-time sorted result, LINQ is usually very readable. If you want to permanently reorder the existing , is often the better fit.
Mental Model
Think of a list of Order objects like a stack of paper order forms.
Each form has multiple fields:
- Order ID
- Date
- Quantity
- Total
Sorting means choosing one field to look at and arranging all forms by that field.
- If you sort by
OrderDate, you line them up from oldest to newest. - If you sort by
OrderId, you line them up by ID number. - If you sort by
Total, you line them up by price.
So the object is the full form, and the property is the specific column you use to organize it.
Syntax and Examples
Using List<T>.Sort()
Use Sort() when you want to reorder the original list.
objListOrder.Sort((x, y) => x.OrderId.CompareTo(y.OrderId));
This compares the OrderId of two Order objects and sorts them in ascending order.
Sort by date
objListOrder.Sort((x, y) => x.OrderDate.CompareTo(y.OrderDate));
This sorts the list from earliest date to latest date.
Using LINQ OrderBy()
Use OrderBy() when you want a sorted result without modifying the original list.
var sortedById = objListOrder.OrderBy(o => o.OrderId).ToList();
Sort by date
var sortedByDate = objListOrder.OrderBy(o => o.OrderDate).ToList();
Descending order
var latestFirst = objListOrder.OrderByDescending(o => o.OrderDate).ToList();
Step by Step Execution
Consider this code:
var sorted = objListOrder.OrderBy(o => o.OrderDate).ToList();
Here is what happens step by step:
objListOrderis the original list ofOrderobjects.OrderBy(o => o.OrderDate)tells C# to use each order'sOrderDateas the sorting key.- LINQ examines each item and reads its
OrderDate. - It arranges the items from smallest to largest date.
- earlier dates come first
- later dates come after
ToList()creates a newList<Order>containing the sorted items.- The original
objListOrderis unchanged unless you assign the result back.
Example:
var orders = new List<Order>
{
new Order { OrderId = 10, OrderDate = new DateTime(2024, 5, 1) },
new Order { OrderId = 11, OrderDate = new DateTime(, , ) },
Order { OrderId = , OrderDate = DateTime(, , ) }
};
sorted = orders.OrderBy(o => o.OrderDate).ToList();
Real World Use Cases
Sorting object lists by properties is very common in real applications.
Common scenarios
- E-commerce systems: sort orders by date, total amount, or status
- Admin dashboards: show newest records first
- Reports: sort invoices by customer, month, or amount
- APIs: prepare data in a predictable order before returning it
- Desktop apps: display rows in sorted grids or tables
- Data processing scripts: process oldest or highest-priority items first
Examples
Show newest orders first
var recentOrders = orders
.OrderByDescending(o => o.OrderDate)
.ToList();
Show highest-value orders first
var topOrders = orders
.OrderByDescending(o => o.Total)
.ToList();
Sort for cleaner reporting
var reportOrders = orders
.OrderBy(o => o.OrderDate)
.ThenBy(o => o.OrderId)
.ToList();
Real Codebase Usage
In real codebases, sorting is often combined with filtering, validation, and display logic.
Common patterns
1. Sort after filtering
var openOrders = orders
.Where(o => o.Total > 0)
.OrderBy(o => o.OrderDate)
.ToList();
Developers often filter first, then sort the remaining data.
2. Use early checks before sorting
if (orders == null || orders.Count == 0)
{
return new List<Order>();
}
var sortedOrders = orders.OrderBy(o => o.OrderId).ToList();
This prevents errors and makes the code safer.
3. Sort by user-selected columns
string sortBy = "OrderDate";
var sorted = sortBy switch
{
"OrderId" => orders.OrderBy(o => o.OrderId).ToList(),
"Total" => orders.OrderBy(o => o.Total).ToList(),
_ => orders.OrderBy(o => o.OrderDate).ToList()
};
This pattern appears in APIs, grid views, and search pages.
4. Stable multi-column sorting for UI tables
var sorted = orders
.OrderBy(o => o.OrderDate)
.ThenBy(o => o.OrderId)
.ToList();
Common Mistakes
1. Forgetting that OrderBy() does not change the original list
Broken assumption:
objListOrder.OrderBy(o => o.OrderId);
This does not sort objListOrder in place.
Correct:
objListOrder = objListOrder.OrderBy(o => o.OrderId).ToList();
Or use Sort():
objListOrder.Sort((x, y) => x.OrderId.CompareTo(y.OrderId));
2. Using Sort() on something that is not a List<T>
Sort() is a method on List<T>, not on every collection type.
If you have an IEnumerable<Order>, use LINQ:
var sorted = orders.OrderBy(o => o.OrderDate).ToList();
3. Comparing the wrong types manually
Broken code:
objListOrder.Sort((x, y) => x.OrderDate > y.OrderDate ? : );
Comparisons
| Approach | Modifies original list? | Returns new sequence? | Best for |
|---|---|---|---|
List<T>.Sort() | Yes | No | Reordering an existing List<T> |
OrderBy() | No | Yes | Readable query-style sorting |
OrderByDescending() | No | Yes | Reverse ordering |
ThenBy() | No | Yes | Secondary sorting |
Sort() vs OrderBy()
Cheat Sheet
Quick syntax
Sort existing list in place
objListOrder.Sort((x, y) => x.OrderId.CompareTo(y.OrderId));
Sort by date in place
objListOrder.Sort((x, y) => x.OrderDate.CompareTo(y.OrderDate));
Create a new sorted list
var sorted = objListOrder.OrderBy(o => o.OrderId).ToList();
Descending order
var sorted = objListOrder.OrderByDescending(o => o.OrderDate).ToList();
Sort by multiple properties
var sorted = objListOrder
.OrderBy(o => o.OrderDate)
.ThenBy(o => o.OrderId)
.ToList();
Rules to remember
Sort()changes the originalList<T>.OrderBy()returns a sorted sequence and does not change the original list.- Use
ToList()if you want a newList<T>fromOrderBy().
FAQ
How do I sort a List<Order> by a property in C#?
Use either:
objListOrder.Sort((x, y) => x.OrderId.CompareTo(y.OrderId));
or:
var sorted = objListOrder.OrderBy(o => o.OrderId).ToList();
What is the difference between Sort() and OrderBy() in C#?
Sort() changes the original List<T>. OrderBy() returns a new sorted sequence and leaves the original collection unchanged unless you assign the result.
How do I sort a list of objects by date in C#?
var sorted = objListOrder.OrderBy(o => o.OrderDate).ToList();
For descending order:
var sorted = objListOrder.OrderByDescending(o => o.OrderDate).ToList();
How do I sort by more than one property?
Use ThenBy() or ThenByDescending():
Mini Project
Description
Build a small order-reporting tool that sorts customer orders in different ways. This project demonstrates how object-property sorting is used in real programs, such as admin dashboards and report generation.
Goal
Create a C# program that stores a list of orders and displays them sorted by ID, date, and total.
Requirements
- Create an
Orderclass withOrderId,OrderDate,Quantity, andTotalproperties. - Add at least five sample
Orderobjects to a list. - Display the orders sorted by
OrderIdin ascending order. - Display the orders sorted by
OrderDatein descending order. - Display the orders sorted by
Total, and useOrderIdas a tie-breaker. - Print the sorted results to the console clearly.
Keep learning
Related questions
AddTransient vs AddScoped vs AddSingleton in ASP.NET Core Dependency Injection
Learn the differences between AddTransient, AddScoped, and AddSingleton in ASP.NET Core DI with examples and practical usage.
C# Type Checking Explained: typeof vs GetType() vs is
Learn when to use typeof, GetType(), and is in C#. Understand exact type checks, inheritance, and safe type testing clearly.
C# Version Numbers Explained: C# vs .NET Framework and Why “C# 3.5” Is Incorrect
Learn the correct C# version numbers, how they map to .NET releases, and why terms like C# 3.5 are inaccurate and confusing.