How to Sort List<T> by Object Property in C#: Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Sorting List<T> in C#: From Chaos to Order in 5 Minutes

\n\n

Imagine: you have a list of users, and you need to display them on screen in alphabetical order by last name. Or a list of products — sort by price from cheapest to most expensive. A familiar task? On Stack Overflow, the question «How to Sort a List<T> by a property in the object» has gathered thousands of views — meaning this topic concerns many developers.

\n\n

In this article, we will cover five ways to sort a list in C#. You will learn how to sort by a single property, multiple fields, and also how to make sorting as fast as possible. Let's go!

\n\n

What We Will Sort: The User Class

\n\n

For examples, let's take a simple data model — a user with a name, age, and rating.

\n\n
public class User\n{\n    public string Name { get; set; }\n    public int Age { get; set; }\n    public double Rating { get; set; }\n\n    public override string ToString()\n    {\n        return $"{Name} ({Age}) — {Rating:F2}";\n    }\n}
\n\n

Let's create a test list:

\n\n
List<User> users = new List<User>\n{\n    new User { Name = "Anna", Age = 30, Rating = 4.5 },\n    new User { Name = "Boris", Age = 25, Rating = 3.8 },\n    new User { Name = "Victor", Age = 35, Rating = 4.9 },\n    new User { Name = "Galina", Age = 28, Rating = 4.2 }\n};
\n\n

Method 1: LINQ OrderBy — The Simplest and Most Elegant

\n\n

If you want to sort List<T> by an object property quickly and without extra code — use LINQ. The OrderBy method creates a new ordered sequence without modifying the original list. This is ideal when you need sorting "on the fly".

\n\n
// Sort by name (ascending)\nvar sortedByName = users.OrderBy(u => u.Name).ToList();\n\n// Sort by age (descending)\nvar sortedByAgeDesc = users.OrderByDescending(u => u.Age).ToList();\n\n// Sort by multiple fields: first by age, then by rating\nvar sortedMulti = users\n    .OrderBy(u => u.Age)\n    .ThenBy(u => u.Rating)\n    .ToList();
\n\n

Pros: readability, sorting chains, works with any property types.
\nCons: creates a new list (additional memory).

\n\n

Method 2: List.Sort with Comparison Delegate — In-Place Sorting

\n\n

If you need to sort a C# list without creating a copy (memory saving), use the Sort method with a lambda expression. This method modifies the original collection.

\n\n
// Sort by name (in place)\nusers.Sort((a, b) => string.Compare(a.Name, b.Name, StringComparison.Ordinal));\n\n// Sort by age (descending)\nusers.Sort((a, b) => b.Age.CompareTo(a.Age));\n\n// Sort by rating\nusers.Sort((a, b) => a.Rating.CompareTo(b.Rating));
\n\n

Pros: does not create a new object, faster for large lists.
\nCons: less readable for complex sorting.

\n\n

Method 3: Implementing IComparable — Sorting "Out of the Box"

\n\n

If your class should always be sorted by the same field (e.g., by name), implement the IComparable<T> interface. Then List.Sort() will work without arguments.

\n\n
public class User : IComparable<User>\n{\n    // ... properties from the example above ...\n\n    public int CompareTo(User other)\n    {\n        if (other == null) return 1;\n        return string.Compare(this.Name, other.Name, StringComparison.Ordinal);\n    }\n}\n\n// Usage\nusers.Sort(); // automatically by name
\n\n

Pros: natural sorting for business logic.
\nCons: hardcodes a single rule — for other fields you'll need to write separate logic.

\n\n

Method 4: IComparer — Flexible Sorting

Blogs

Book Recommendations