Given:
List<DateTime> descOrderedDates;
And a datetime:
DateTime fromDate;
I can easily count the number of items where the date is greater than fromDate
as follows:
var count = descOrderedDates.Count(c=> c > fromDate);
However I'm struggling to implement this as a BinarySearch
:
var ix = descOrderedDates.BinarySearch(fromDate, new CompareDates());
private class CompareDates : IComparer<DateTime>
{
public int Compare(DateTime compareDate, DateTime fromDate)
{
if (fromDate > compareDate) return 1;
return 0;
}
}
This keeps returning 1
in a test case where fromDate
is less than the smallest date in the list. I'm struggling to wrap my head around IComparer
, can anyone tell me what I'm doing wrong?