C# Array BinarySearch(T[], T, IComparer)

Description

Array BinarySearch (T[], T, IComparer ) searches an entire one-dimensional sorted array for a value using the specified IComparer generic interface.

Syntax

Array.BinarySearch<T>(T[], T, IComparer<T>) has the following syntax.


public static int BinarySearch<T>(
  T[] array,/*from   w w  w .j  av  a2  s .c o  m*/
  T value,
  IComparer<T> comparer
)

Parameters

Array.BinarySearch<T>(T[], T, IComparer<T>) has the following parameters.

  • T - The type of the elements of the array.
  • array - The sorted one-dimensional, zero-based Array to search.
  • value - The object to search for.
  • comparer - The IComparer implementation to use when comparing elements.
  • comparer - -or-
  • comparer - null to use the IComparable implementation of each element.

Returns

Array.BinarySearch<T>(T[], T, IComparer<T>) method returns The index of the specified value in the specified array, if value is found. If value is not found and value is less than one or more elements in array, a negative number which is the bitwise complement of the index of the first element that is larger than value.

Example

The following example demonstrates the Sort<T>(T[], IComparer<T>) generic method overload and the BinarySearch<T>(T[], T, IComparer<T>) generic method overload.


/*from   w w w  . j  av a 2  s .c o  m*/
using System;
using System.Collections.Generic;

public class ReverseComparer: IComparer<string>
{
    public int Compare(string x, string y)
    {
        // Compare y and x in reverse order. 
        return y.CompareTo(x);
    }
}

public class Example
{
    public static void Main()
    {
        string[] myValues = {"2016", 
                              "1999", 
                              "2001", 
                              "2000", 
                              "2002", 
                              "2003"};

        ReverseComparer rc = new ReverseComparer();
        Array.Sort(myValues, rc);

        foreach( string myValue in myValues )
        {
            Console.WriteLine(myValue);
        }

        int index = Array.BinarySearch(myValues, "2011", rc);
        ShowWhere(myValues, index);

        index = Array.BinarySearch(myValues, "2001", rc);
        ShowWhere(myValues, index);
    }

    private static void ShowWhere<T>(T[] array, int index)
    {
        if (index<0)
        {
            index = ~index;

            Console.Write("Not found. Sorts between: ");

            if (index == 0)
                Console.Write("beginning of array and ");
            else
                Console.Write("{0} and ", array[index-1]);

            if (index == array.Length)
                Console.WriteLine("end of array.");
            else
                Console.WriteLine("{0}.", array[index]);
        }
        else
        {
            Console.WriteLine("Found at index {0}.", index);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version