6

I have an object named Artist.

public class Artist
{
    public string artistID { get; set; }
    public string artistName { get; set; }

    public Artist(string artistID, string artistName)
    {
        this.artistID = artistID;
        this.artistName = artistName;

    }

    public Artist()
    {
    }
    public Artist(string artistID)
    {
        this.artistID = artistID;
    }

Which has a custom Comparer like this:

public class CompareArtists : IComparer<Artist>
{

  public int Compare(Artist x, Artist y)
  {
      if (x.artistID == null) return -1;
      if (y.artistID == null) return 1;

    return String.Compare(x.artistID, y.artistID);
   }

}

I am trying to see whether an artist is contained in my list of artists in the main program. And I am doing that like this:

Artist tempArtist = new Artist(user[1]);
Artist result = new Artist();

position = artists.BinarySearch(tempArtist, dc);
result = artists.ElementAt(position);
                       users.ElementAt(counter).favoriteArtists.Add(result,Int32.Parse(user[2]));

The result of the binarysearch, is a negative number which from what I read on MSDN, is the complement of the next largest index. And it is shown when no results were found. However, I am 100% certain that the item I am searching for in my artists list does actually exist there. When I use a linear method, e.g. artists.Single or artists.Find, I get a result.

Do you see anything wrong with my comparer or the way I use binary search?

ps: dc is my comparer: CompareArtists dc = new CompareArtists(); and user[] is an array of strings that were split from a line I read with stream reader. it cotains the artist ID at [1] thanks in advance

4
  • 2
    Is your list sorted? Because you can use BinarySearch on only a sorted list. Commented Feb 22, 2014 at 23:09
  • 1
    Also note that your comparer does the wrong thing if it's presented with two artists both of whom have a null ID. It should return 0 in that case. Commented Feb 22, 2014 at 23:10
  • The list was generated sequentially by reading a file where the IDs are actually ordered. So I would say yes. If that is what you mean Commented Feb 22, 2014 at 23:11
  • What is dc? Dinocomparer? Commented Dec 29, 2020 at 14:38

1 Answer 1

4

You need to sort your list before BinarySearch, see documentation

Searches the entire sorted List for an element using the specified comparer and returns the zero-based index of the element.

artists.Sort(new CompareArtists());
position = artists.BinarySearch(tempArtist, dc);

Also you should use same comparer in Sort and BinarySearch to make it work

Sign up to request clarification or add additional context in comments.

2 Comments

You were all right, I imagined that because the list was generated sequentially, it would actually be ordered. But upon sorting the artists list, my code works. I am confused, but it works.
Quick followup. I was actually comparing them as string, in my sequential generation they were ordered as Integers. As strings, the ordering is different. That is why it did not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.