C# LINQ OrderBy
Sort workshop rows by count and then code so equal primary values have a documented display order.
How it works
OrderBy defines a primary ordering and ThenBy adds a secondary key. Calling OrderBy again starts a new primary ordering rather than extending the previous one. A deterministic report should state its tie-break policy, especially when the source itself has no useful order. String comparison also needs a deliberate culture or ordinal choice. LINQ ordering materializes internal work on enumeration and is not a constant-time view of arbitrary input.
Run it locally
Use the .NET 10 SDK with C# 14 in a separate console lab. Create an empty project once with dotnet new console --framework net10.0, then replace Program.cs with this complete source and add any supporting files shown. Keep only one lesson entry point in the project. C# does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed.
dotnet build -p:LangVersion=14.0 -p:Nullable=enable -p:TreatWarningsAsErrors=true
dotnet run --no-buildUse the SDK’s project workflow; the review compiles against its matching reference assemblies and executes the results without external NuGet packages. File-writing examples belong in a private empty lab directory.
Example
using System;
using System.Linq;
internal static class Program
{
private static void Main(string[] args)
{
var input = new[] { new Row("W-2", 4), new Row("W-1", 4), new Row("W-3", 2) };
var report = input.OrderByDescending(row => row.Count).ThenBy(row => row.Code, StringComparer.Ordinal);
foreach (var row in report) Console.WriteLine($"{row.Code}: {row.Count}");
}
}
internal sealed record Row(string Code, int Count);
Expected result
W-1: 4
W-2: 4
W-3: 2
Both four-place rows appear before the two-place row, and their ordinal code order resolves the tie. The query returns existing immutable row references rather than rewriting their values. For large data, include sorting’s allocation and growth cost in the design instead of judging only the short call syntax.
Consult Microsoft’s C# documentation for language and library contracts, including target-framework and version requirements.