List(T).BinarySearch Method (T, IComparer(T))
Searches the entire sorted List(T) for an element using the specified comparer and returns the zero-based index of the element.
Assembly: mscorlib (in mscorlib.dll)
- Type: T
The object to locate. The value can be a null reference (Nothing in Visual Basic) for reference types.
The comparer customizes how the elements are compared. For example, you can use a CaseInsensitiveComparer instance as the comparer to perform case-insensitive string searches.
If comparer is a null reference (Nothing in Visual Basic), the default comparer Comparer(T).Default checks whether type T implements the IComparable(T) generic interface and uses that implementation, if available. If not, Comparer(T).Default checks whether type T implements the IComparable interface. If type T does not implement either interface, Comparer(T).Default throws InvalidOperationException.
The List(T) must already be sorted according to the comparer implementation; otherwise, the result is incorrect.
Comparing a null reference (Nothing in Visual Basic) with any reference type is allowed and does not generate an exception when using the IComparable(T) generic interface. When sorting, a null reference (Nothing in Visual Basic) is considered to be less than any other object.
If the List(T) contains more than one element with the same value, the method returns only one of the occurrences, and it might return any one of the occurrences, not necessarily the first one.
If the List(T) does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operation (~) to this negative integer to get the index of the first element that is larger than the search value. When inserting the value into the List(T), this index should be used as the insertion point to maintain the sort order.
This method is an O(log n) operation, where n is the number of elements in the range.
The following example demonstrates the Sort(IComparer(T)) method overload and the method overload.
The example defines an alternative comparer for strings named DinoCompare, which implements the IComparer<string> (IComparer(Of String) in Visual Basic, IComparer<String^> in Visual C++) generic interface. The comparer works as follows: First, the comparands are tested for a null reference (Nothing in Visual Basic), and a null reference is treated as less than a non-null. Second, the string lengths are compared, and the longer string is deemed to be greater. Third, if the lengths are equal, ordinary string comparison is used.
A List(T) of strings is created and populated with four strings, in no particular order. The list is displayed, sorted using the alternate comparer, and displayed again.
The method overload is then used to search for several strings that are not in the list, employing the alternate comparer. The Insert method is used to insert the strings. These two methods are located in the function named SearchAndInsert, along with code to take the bitwise complement (the ~ operator in C# and Visual C++, Xor -1 in Visual Basic) of the negative number returned by and use it as an index for inserting the new string.
.NET FrameworkSupported in: 4.5, 4, 3.5, 3.0, 2.0
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
Portable Class LibrarySupported in: Portable Class Library
.NET for Windows Store appsSupported in: Windows 8
.NET for Windows Phone appsSupported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.