I have an array A of objects, each with the public field Value (double) which have random doubles between 0 and 1. A is sorted by this field. I create double random = 0.25. Now I want to find the first object from A with A[index].Value >= random. Can I do this with int index = Array.BinarySearch() in some way?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Here is an implementation of
BinarySearch
that you can use. In addition to the other arguments that would normally be accepted, it also accepts aselector
which determines the actual object that should be compared for each item, and for the value to find it accepts a value of that type, rather than the type of the array.To use it is simple enough:
Best way would be to roll your own.
Usage:
Based on Can LINQ use binary search when the collection is ordered?