Custom Sorting (IComparer on three fields)

//Assuming all the fields implement IComparable
int result = a.field1.CompareTo(b.field1);
if (result == 0)
  result = a.field2.CompareTo(b.field2);
if (result == 0)
  result = a.field3.CompareTo(b.field3);
return result;

Sort on one field at a time, in order of precedence, only continuing to the next field if the previous field compare resulted in 0 (fields equal). See the following for an example of a 2-field sort.

http://csharp.2000things.com/2010/10/30/135-implementing-icomparable-to-allow-sorting-a-custom-type/


I don't know what's the use you have for the comparer, but maybe you could use instead of a comparer the "order by" LINQ statement, which allows to sort by various fields:

var orderedListPersons =
    from p in listPersons
    orderby p.Title, p.Name, p.Gender
    select person;

will order listPersons the way you want. You can also use the LINQ OrderBy and ThenBy methods for the same thing with a different syntax:

var orderedlistPersons = listPersons.OrderBy(p => p.Title).ThenBy(p => p.Name).ThenBy(p => p.Gender);