I have a collection and want to return the nearest value to some fixed value but not exceed the fixed value. For example, if my collection looks like this:
val numbers = mutableListOf(23, 12, 64, 47, 36, 55)
and I have a target fixed value of 35, the value returned from the collection would be 23. Here are some other examples:
Target -> Returned
29 -> 23
5 -> null (there is no value less than 12, so null is returned)
70 -> 64
Is there some Collection function that I can use that would provide the expected result? Note: The list of numbers is not sorted. In the real app, these are not numbers but objects that do contain an integer property, but I could just as well sort the collection first on that value if that would help in the solution.