I have a really annoying error whereby my comparer.Compare(x, y) is not getting called. I have an IList that is returning a bunch of entities from the database and then I'm sorting a list of entities inside each entity returned from the database.
ie: in this example each household has many accounts, and i want to sort the sub-list by properties on the accounts entities.
My calling logic is as follow:
List<Household> households = query.ToList();
households.Sort(new HouseholdComparer());
return households;
and my comparer looks like this:
public class HouseholdComparer : IComparer<Household>
{
public int Compare(Household x, Household y)
{
foreach (Account xAccount in x.Accounts)
{
foreach (Account yAccount in y.Accounts)
{
if (xAccount.StartDate == yAccount.StartDate)
{
if ((xAccount.RevenueT12.HasValue && yAccount.RevenueT12.HasValue)
&& (xAccount.RevenueT12.Value == yAccount.RevenueT12.Value))
{
if ((xAccount.AUAAnnual.HasValue && yAccount.AUAAnnual.HasValue)
&& (xAccount.AUAAnnual.Value == yAccount.AUAAnnual.Value))
return 0; // all same whatever result
if (!xAccount.AUAAnnual.HasValue || !yAccount.AUAAnnual.HasValue) return 0;
if (xAccount.AUAAnnual.Value > yAccount.AUAAnnual.Value) return 1;
if (xAccount.AUAAnnual.Value < yAccount.AUAAnnual.Value) return -1;
}
else
{
if (!xAccount.RevenueT12.HasValue || !yAccount.RevenueT12.HasValue) return 0;
if (xAccount.RevenueT12.Value > yAccount.RevenueT12.Value) return 1;
if (xAccount.RevenueT12.Value < yAccount.RevenueT12.Value) return -1;
}
}
else
{
if (x.StartDate > y.StartDate) return 1;
if (x.StartDate < y.StartDate) return -1;
}
}
}
return 0; // it shouldn't get here
}
When I run the debugger I get a hit in the constructor but nothing in the compare method, can anyone help?????