using System.Collections.Generic;
public static void Main()
List<Thing> thingList = new List<Thing>
new Thing { X ="A", Y = "B", Z="C" },
new Thing { X ="X", Y = "Y", Z="Z" },
new Thing { X ="1", Y = "2", Z="3" },
new Thing { X ="!", Y = "@", Z="#" },
Console.WriteLine("thing list order - unsorted");
thingList.ForEach(thing => Console.WriteLine(thing));
.OrderByDescending(t => t.Z == ".")
.ThenByDescending(t => t.Y == "Z")
.ThenByDescending(t => t.X == "!");
Console.WriteLine("try match on 3rd condition X == \"!\"");
Console.WriteLine(q.FirstOrDefault());
.OrderByDescending(t => t.Z == ".")
.ThenByDescending(t => t.Y == "Y")
.ThenByDescending(t => t.X == "!");
Console.WriteLine("try match on 3rd condition Y == \"Y\"");
Console.WriteLine(q.FirstOrDefault());
Console.WriteLine("Changing list order...");
thingList = new List<Thing>
new Thing { X ="1", Y = "2", Z="3" },
new Thing { X ="A", Y = "B", Z="C" },
new Thing { X ="!", Y = "@", Z="#" },
new Thing { X ="X", Y = "Y", Z="Z" },
Console.WriteLine("thing list order - changed - it now looks like this");
thingList.ForEach(thing => Console.WriteLine(thing));
.OrderByDescending(t => t.Z == ".")
.ThenByDescending(t => t.Y == "Z")
.ThenByDescending(t => t.X == "!");
Console.WriteLine("try match on 3rd condition X == \"!\"");
Console.WriteLine(q.FirstOrDefault());
.OrderByDescending(t => t.Z == ".")
.ThenByDescending(t => t.Y == "Y")
.ThenByDescending(t => t.X == "!");
Console.WriteLine("try match on 3rd condition Y == \"Y\"");
Console.WriteLine(q.FirstOrDefault());
public string X { get; set; }
public string Y { get; set; }
public string Z { get; set; }
public override string ToString()
return string.Format("X: {0} Y: {1} Z: {2}", X, Y, Z);