Java BinarySearch Comparator Example

I'm trying to find an example of a binarysearch comparator that finds a string in an arraylist.  I need the compareTo() example as well.  Appreciate it.
ihateuselessitbooksAsked:
Who is Participating?
 
Thomas4019Commented:
Here is an example showing a binary search on an ArrayList.

http://www.java-examples.com/perform-binary-search-java-arraylist-example
0
 
Thomas4019Commented:
That example uses the built in Collections.binarySearch()
0
 
ihateuselessitbooksAuthor Commented:
been there, done that.  I'm after the Comparator and compareTo(), not the call to the comparator.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Thomas4019Commented:
The class java "Comparator" is designed for you to specify a way of comparing objects. Such as that you could make a CustomComparator which has its own system for deciding whether object A is less than or greater than object B.

Or if you are making a custom object you can implement the "Comparable" interface. This mandates that you supply the function that tells whether an object is less than or greater than another object.

There is no such thing as a "Binary Search Comparator" if you are talking about comparators as they are used in Java. You could make a class which does binary searching named Comparator, of course, but you can not do a binary search with only comparators.

Comparators and Comparable are only designed for specifing the mehod to compare objects.

http://www.onjava.com/pub/a/onjava/2003/03/12/java_comp.html
http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html
0
 
ihateuselessitbooksAuthor Commented:
thanks for your help, but still looking here.
0
 
Thomas4019Commented:
what exactly are you looking for?
0
 
Thomas4019Commented:
Java custom implementaion of a binary search. (not using Collections.binarySearch())

http://www.java-tips.org/java-se-tips/java.lang/binary-search-implementation-in-java.html
0
 
Thomas4019Commented:
0
 
ihateuselessitbooksAuthor Commented:
Thanks for the help.  Actually looking for an example of a binarySearch comparator called on the order of Collections.sort.   Being somewhat new in Java, I probably didn't even ask the right question(s) completely, but I did figure out something after pulling my hair out a few days.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.