Lambda Expressions, Compare item1, if they are equal compare item2

I tried to search but I cannot seem to find my answer. I think an answer may exist as it not a uncommon question. I trying to say Sort by Item1. If they are equal, sort by Item2

sorted.Sort((a,b)=>(a.Item1.CompareTo(b.Item1)));
Jon Skeet
people
quotationmark

While you can build a comparer to do this with List<T>.Sort, it's much easier to use LINQ, which is built for this sort of thing:

sorted = unsorted.OrderBy(x => x.Item1).ThenBy(x => x.Item2).ToList();

If you really want to use Sort, you can use the ProjectionEqualityComparer in my MiscUtil project - but it won't be as nice as the LINQ approach.

people

See more on this question at Stackoverflow