Binary Search Two Values

The built in routines only search for a single value - you could however implement your own search routines that could look for multiple entries at a time but I would imagine that could get quite complex if you are implementing it correctly.

Is there any reason why you couldn't just do two binary searches?
 
I think my bad communication skills have struck again. I don't want to return two objects in the array rather find the customer object where country=usa and state=tn for example.
 
Assuming you are using an IComparer for your search (say through an ArrayList.BinarySearch) you will simply need to specify the criterea by which the IComparer will return greater than, less than or equals for the fields in the customer object you are interested in. I'll try an quick example of what this might look like.
C#:
    public class Class2 : IComparer
    {
        #region IComparer Members

        /// <summary>
        /// Compares two objects (assumed to be Customers) based on country then state.
        /// </summary>
        /// <param name="x">The first customer to compare</param>
        /// <param name="y">The second customer to compare</param>
        /// <returns>0 if x and y are equal, a positive number if x is greater than y, a negative number if x is less than y.</returns>
        /// <remarks>It is assumed that the list is ordered by country first, then state.  This does not take
        /// into account if a country does not have states.</remarks>
        public int Compare(object x, object y)
        {
            Customer c1 = (Customer)x;
            Customer c2 = (Customer)y;

            int countryCompare = String.Compare(c1.Country, c2.Country);
            if (coutnryCompare == 0) //the countries are the same.
            {
                return String.Compare(c1.State, c2.State);
            }

            return countryCompare; //Since they weren't equal, return the differnce.
        }

        #endregion
    }
If you are using VS2005, you might want to consider using generics, but if you aren't don't worry about it.
 
Back
Top