Sorting List<T> in C#: From Chaos to Order in 5 Minutes
\n\nImagine: 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\nIn 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\nWhat We Will Sort: The User Class
\n\nFor examples, let's take a simple data model — a user with a name, age, and rating.
\n\npublic 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\nLet's create a test list:
\n\nList<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\nMethod 1: LINQ OrderBy — The Simplest and Most Elegant
\n\nIf 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".
// 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\nPros: readability, sorting chains, works with any property types.
\nCons: creates a new list (additional memory).
Method 2: List.Sort with Comparison Delegate — In-Place Sorting
\n\nIf 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.
// 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\nPros: does not create a new object, faster for large lists.
\nCons: less readable for complex sorting.
Method 3: Implementing IComparable — Sorting "Out of the Box"
\n\nIf 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.
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\nPros: natural sorting for business logic.
\nCons: hardcodes a single rule — for other fields you'll need to write separate logic.