Results 1 to 1 of 1
Thread: How to search with a Comparator
-
How to search with a Comparator
Java Code:import java.util.Arrays; import java.util.Comparator; public class AlphabeticSearch { public static void main(String[] args) { String[] sa = new String[] { "a", "c", "d" }; AlphabeticComparator comp = new AlphabeticComparator(); Arrays.sort(sa, comp); int index = Arrays.binarySearch(sa, sa[10], comp); System.out.println("Index = " + index); } } ///:~ class AlphabeticComparator implements Comparator { public int compare(Object o1, Object o2) { String s1 = (String) o1; String s2 = (String) o2; return s1.toLowerCase().compareTo(s2.toLowerCase()); } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
How to write your own Comparator
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:38 PM -
Search
By Gilgamesh in forum New To JavaReplies: 5Last Post: 11-26-2007, 03:45 AM -
Using Comparable and Comparator interfaces
By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:10 AM -
Web Search
By Marcus in forum Enterprise JavaBeans (EJB)Replies: 2Last Post: 07-02-2007, 06:51 AM -
Search in hibernate
By Nick15 in forum JDBCReplies: 4Last Post: 05-09-2007, 01:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks