I have a list of numbers, I would like to get the next >=
number than the one I pass, but there's a problem when I pass value that's bigger to the biggest in list. For example when I pass 41
and the biggest in list is 40 it won't work so I'd like it to return 40.
var numbers = new[] {30, 20, 40};
I would like it to work like:
numbers.GetNearest(45) -> 40
numbers.GetNearest(40) -> 40
numbers.GetNearest(31) -> 40
numbers.GetNearest(30) -> 30
numbers.GetNearest(29) -> 30
numbers.GetNearest(1) -> 20
Is this possible with LINQ alone, or is there any well-known algorithm for this?
What I have now is something like:
numbers.OrderBy(n => n).FirstOrDefault(n => n >= minute)
but it doesn't work when I pass value bigger than 40
I don't want the closest number, but the next one >=
than the one I pass, but for certain numbers bigger than the biggest in list won't work so I'd like to return just the last one.