1

I'm still pretty green at programming and am running into an issue with the syntax of binary search on an array in Java. I'm trying to invoke a comparator method (overloaded "compare" method) that exists in a separate class from the class that I am using the binary search in. Essentially my goal is to search the array for only one of the variables stored in the object that makes up the array. Without the comparator I have been unsuccessful in doing this as I have created a "dummy" object to hold only the criteria needed for the search as the key.

Here is my code for the binary search:

Song searchSong = new Song(artistInput, artistInput, artistInput);
int search = Arrays.binarySearch(songs, searchSong, new compare<Song>());

This is my code for the overloaded comparator, again, in a separate class:

public int compare (Song firstSong, Song secondSong) {
  return firstSong.getArtist().compareTo(secondSong.getArtist());
}

I'm sure it's just something simple I'm missing, but I've yet to find the answer. I appreciate any help and if more specifics are needed, please let me know. I know that the code for the binary search does not work in it's current form.

3
  • 1) Which is the problem (compilation error/runtime error/wrong result)? 2) Does your class extend java.util.Comparator? and 3) All Class Names In Java Begin With Capital Letter Commented Sep 24, 2012 at 17:30
  • Sorry but what exactly is your question? You may be new to stackoverflow and programming but you do realize that you have to ask a question right? I am guessing you meant override and not overload right? And yea, the code snippets you have provided won't compile unless compare is a class. In which case, you should name it with a capital letter. This may all sound harsh but you need to get your basics right before jumping into API's like binary search. Commented Sep 24, 2012 at 17:36
  • compare is a method, not a class. Sorry if I wasn't clear. And yes, I meant override, not overload. Commented Sep 24, 2012 at 18:10

2 Answers 2

6

try

 Song searchSong = new Song(artistInput, artistInput, artistInput);
 int search = Arrays.binarySearch(songs, searchSong, new Comparator<Song>(){
    @Override
    public int compare(Song s1, Song s2) {
      return s1.getArtist().compareTo(s2.getArtist());
    }
 });

UPDATE for Java 8:

 int search = Arrays.binarySearch(songs, searchSong, 
   (Song s1, Song s2) -> s1.getArtist().compareTo(s2.getArtist()));
Sign up to request clarification or add additional context in comments.

Comments

1

Is the name of your class implementing Comparator compare??

If you have your comparator class like this: -

public class MyComparator implements Comparator<Song> {
     /** Your compare method ***/
}

Your call to Arrays.binarySearch() should take new MyComparator() as parameter..

2 Comments

I apologize, I guess I wasn't very clear. The class Song implements Comparator. I overrode the compare method with my own compare method within that class. From another class I am calling binary search and attempting to use the compare method that I wrote in the Song class. However, I believe I have found what I was doing incorrectly. Thanks for the feedback.
Its Ok.. Sometimes things gets messed up.. At the end of the day, what matters is whether your problem is solved or not.

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.