compareTo( ) in Java

By: Mashoud Viewed: 87 times  Printer Friendly Format    


Often, it is not enough to simply know whether two strings are identical. For sorting applications, you need to know which is less than, equal to, or greater than the next. A string is less than another if it comes before the other in dictionary order. A string is greater than another if it comes after the other in dictionary order. The String method compareTo( ) serves this purpose. It has this general form:

int compareTo(String str)

Here, str is the String being compared with the invoking String. The result of the comparison is returned and is interpreted as shown here:

Value                   Meaning 
Less than zero          The invoking string is less than str.
Greater than zero		The invoking string is greater than str. 
Zero                    The two strings are equal.

Here is a sample program that sorts an array of strings. The program uses compareTo( ) to determine sort ordering for a bubble sort:

// A bubble sort for Strings. 
class SortString { 
static String arr[] = { 
"Now", "is", "the", "time", "for", "all", "good", "men", 
"to", "come", "to", "the", "aid", "of", "their", "country" 
};

public static void main(String args[]) { 
for(int j = 0; j < arr.length; j++) { 
for(int i = j + 1; i < arr.length; i++) { 
if(arr[i].compareTo(arr[j]) < 0) { 
String t = arr[j]; 
arr[j] = arr[i]; 
arr[i] = t; 
} 
} 
System.out.println(arr[j]); 
} 
} 
}

The output of this program is the list of words:

Now 
aid 
all 
come 
country 
for 
good 
is 
men 
of 
the 
the 
their 
time 
to 
to

As you can see from the output of this example, compareTo( ) takes into account uppercase and lowercase letters. The word "Now" came out before all the others because it begins with an uppercase letter, which means it has a lower value in the ASCII character set.

If you want to ignore case differences when comparing two strings, use compareToIgnoreCase( ), shown here:

int compareToIgnoreCase(String str);

This method returns the same results as compareTo( ), except that case differences are ignored. This method was added by Java 2. You might want to try substituting it into the previous program. After doing so, "Now" will no longer be first.



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. how i can used inheritence ?
View Tutorial          By: samee at 2008-02-16 12:54:27

2. Hi you can find inheritance example in Java in the
View Tutorial          By: Mashoud at 2008-04-26 20:12:37

3. Waste code. doesnot work.Array index out of bounds
View Tutorial          By: Tester at 2009-02-26 02:37:09

4. Superb

many thanks to you Mashoud.

View Tutorial          By: orchidouest at 2009-05-10 12:58:53

5. know someone how to compare various objects?

View Tutorial          By: Peter at 2009-10-07 07:32:31

6. Example:

String sOne = "hello

View Tutorial          By: Adam at 2009-11-10 12:55:51

7. @Adam:
CompareTo returns the difference of

View Tutorial          By: Azher at 2010-01-29 12:23:39

8. hi,
Very nice tutorial. But please I have s

View Tutorial          By: hemn at 2010-04-17 06:02:00

9. You talk about "dictionary order". How w
View Tutorial          By: Magnus A at 2010-08-31 22:58:06

10. It is really a nice example when you need to sort
View Tutorial          By: Ranjan at 2010-10-04 23:14:33

11. how about soting you word? like your output will b
View Tutorial          By: fury_2623@yahoo.com at 2011-01-27 16:40:51

12. I am trying to figure out what our Professor did w
View Tutorial          By: Abhishek at 2011-04-09 23:31:28

13. i was told to write a class which wl work as Strin
View Tutorial          By: ireen at 2011-04-13 04:59:31

14. Thik hai
View Tutorial          By: Nosheen Khan at 2011-08-08 06:22:37

15. This is the coding that I wanted...
This is

View Tutorial          By: Damith at 2012-05-16 17:26:10