Jump to content
Xtreme .Net Talk

Recommended Posts

  • Administrators
Posted

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?

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
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.
Posted

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.

    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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...