GenericExtensionsBinarySearchT(IListT, FuncT, Int32) Method
Searches the entire sorted
IListT for an element using the provided
comparer and returns the zero-based index of the element.
This differs from the "ordinary" binary search in allowing a comparer delegate that defines
whether an item is found (returning 0), whether the item in the list is before (<0) or after (>0)
that knows how to compare a class with its key. Example, if the list contains classes of type T having
an id number and the class is sorted on that id, then the keySelector returns the id number for that class.
If having a list of doubles, to find 4.5 in the list, use:
int index = list.BinarySearch(d => d.CompareTo(4.5))
Namespace: DHI.Mike1D.GenericAssembly: DHI.Mike1D.Generic (in DHI.Mike1D.Generic.dll) Version: 24.0.0.0 (11.1.1.1111)
public static int BinarySearch<T>(
this IList<T> list,
Func<T, int> comparer
)
- list IListT
- List to search in
- comparer FuncT, Int32
- A delegate/lambda expression specifying if a given item in the list is before (<0), matches (0) or is after (>0) the item to search for.
- T
- The type of the objects in the list
Int32The zero-based index of item in the sorted
IListT, if item is found;
otherwise, a negative number that is the bitwise complement
of the index of the next element that is larger than item or,
if there is no larger element, the bitwise complement of Count.In Visual Basic and C#, you can call this method as an instance method on any object of type
IListT. When you use instance method syntax to call this method, omit the first parameter. For more information, see
Extension Methods (Visual Basic) or
Extension Methods (C# Programming Guide).