C#
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented C#
-
Section 4: Working with Data
-
Section 5: Error Handling
-
Section 6: Delegates and Events
-
Section 7: Async Programming
-
Section 8: More Language Features
-
Section 9: File I/O and Serialization
-
Section 10: Networking in .NET
-
Section 11: The .NET Ecosystem
-
Section 12: Memory and Performance
-
Section 13: Concurrency Beyond Async
-
Section 14: Reflection and Attributes
-
Section 15: Testing and Best Practices
-
Section 16: Design Patterns in C#
-
Section 17: Standard Library Deep Dive
-
Section 18: Data Structures and Algorithms in C#
-
Section 19: GUI and Desktop Development Overview
-
Section 20: Practical Projects
-
Section 21: More Practice Exercises
-
Section 22: More Standard Library and Text Processing
-
Section 23: More Design Patterns
-
Section 24: More Projects
-
Section 25: Interview Practice
-
Section 26: C# Keywords Reference (Modifiers)
-
Section 27: C# Keywords Reference (Statements)
-
Section 28: C# Keywords Reference (Operators)
-
Section 29: BCL: System.Collections.Generic
-
Section 30: BCL: System.Linq
-
Section 31: BCL: System.Threading
-
Section 32: BCL: System.IO
-
Section 33: BCL: System.Text and System.Text.Json
-
Section 34: BCL: System.Net.Http
-
Section 35: C# Language Specification Topics
-
Section 36: More Practice Exercises
-
Section 37: More Async Patterns
-
Section 38: More BCL: System.Reflection and System.Diagnostics
-
Section 39: Nullable Reference Types In Depth
-
Section 40: C# Records and Pattern Matching In Depth
-
Section 41: Dependency Injection Deep Dive
-
Section 42: More Interview and Whiteboard Practice
188: OrderBy, OrderByDescending, ThenBy
Sorting data is one of those things you'll do in almost every single professional project. In C#, we handle this primarily through LINQ. I'm assuming you've already got the hang of basic lambdas, so we can jump straight into the logic.
How do I actually sort a list?
If you just need a simple sort, you'll use OrderBy for ascending order (A-Z, 1-10) or OrderByDescending for the reverse. Let's say we're building a simple inventory system for a hobby shop.
public class Product
{
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
}
var inventory = new List<Product>
{
new Product { Name = "Acrylic Paint", Category = "Art", Price = 12.50m },
new Product { Name = "Sketchbook", Category = "Art", Price = 15.00m },
new Product { Name = "Gaming Mouse", Category = "Tech", Price = 45.00m },
new Product { Name = "Mechanical Keyboard", Category = "Tech", Price = 80.00m },
new Product { Name = "LED Strip", Category = "Tech", Price = 15.00m }
};
// Sort by price, cheapest first
var cheapestFirst = inventory.OrderBy(p => p.Price);
// Sort by name, Z to A
var reverseAlpha = inventory.OrderByDescending(p => p.Name);
One thing to keep in mind: these methods don't actually change the inventory list itself. They return a new sequence. If you want the result back in a List, you'll need to tack .ToList() onto the end.
What if I need to sort by a second property?
Real-world data is rarely that simple. Usually, you want to sort by a category first, and then sort the items within that category by something else. This is where ThenBy and ThenByDescending come in.
If I want my inventory grouped by category, but I want the most expensive items in each category to appear first, I'd do this:
var sortedInventory = inventory
.OrderBy(p => p.Category)
.ThenByDescending(p => p.Price);
The OrderBy handles the primary sort. When two items have the same category (a "tie"), ThenByDescending steps in to break that tie using the price. It's a clean, readable chain.
Why can't I just call OrderBy twice?
I see this mistake all the time when developers are first starting with LINQ. You might be tempted to do this:
// DON'T DO THIS
var wrongWay = inventory.OrderBy(p => p.Category).OrderBy(p => p.Price);
Here is the catch: OrderBy is a "destructive" sort in terms of the sequence logic. The second OrderBy doesn't supplement the first one; it completely replaces it. In the example above, your list would end up sorted by price, and the category sort would be totally ignored. Always use ThenBy for every subsequent sorting level.
📋 Practical Task
Exercise: Sorting a High-Score Leaderboard
You are building a leaderboard for a game. You have a list of Player objects, each with a Username (string) and a Score (int).
Create a list of at least 5 players, ensuring that at least two players have the exact same score. Write a LINQ query to sort the players based on these rules:
- Primary sort: Score in descending order (highest score at the top).
- Secondary sort: Username in ascending order (alphabetical) to break ties between players with the same score.
Print the final sorted list to the console to verify that the ties were broken correctly by the username.
There are no comments for now.