I have an already sorted list of object (sorted by date)
public class HistoryValue{
public DateTime Date {get;set;}
public decimal Value {get;set;
}
Then I have a list of days, for example,
1MonthAgo, 2MonthAgo,3MonthAgo,120MonthAgo
What I need is to find the Value on date
1MonthAgo, 2MonthAgo,3MonthAgo,120MonthAgo
If the date can not be found in the list, I should return the one just before that date. It is easiest to explain in a SQL statement although I am doing the real work in c#:
select top 1 Value
from HistoryValueList
where Date between @d12m-@lookbackdaymax and @d12m order by Date desc
I was thinking of using binary search, but don't think binary search will do exactly what I want. Maybe it is best do a looping and remember the closet object of each?