compareTo( ) in Java
By: Mashoud
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.
Archived Comments
1. This is the coding that I wanted...
This is the only place that I found this.
View Tutorial By: Damith at 2012-05-16 17:26:10
2. Thik hai
View Tutorial By: Nosheen Khan at 2011-08-08 06:22:37
3. i was told to write a class which wl work as String functions do manually..at a point a method calle
View Tutorial By: ireen at 2011-04-13 04:59:31
4. I am trying to figure out what our Professor did when he was doing this code. I can see he is type c
View Tutorial By: Abhishek at 2011-04-09 23:31:28
5. how about soting you word? like your output will become like this::::!
aid
all
View Tutorial By: [email protected] at 2011-01-27 16:40:51
6. It is really a nice example when you need to sort the object having some strings and some other data
View Tutorial By: Ranjan at 2010-10-04 23:14:33
7. You talk about "dictionary order". How would you sort the strings
"item 11&quo
View Tutorial By: Magnus A at 2010-08-31 22:58:06
8. hi,
Very nice tutorial. But please I have some problems with this function. here is my proble
View Tutorial By: hemn at 2010-04-17 06:02:00
9. @Adam:
CompareTo returns the difference of the ASCII codes of the first non-matching characte
View Tutorial By: Azher at 2010-01-29 12:23:39
10. Example:
String sOne = "hello there";
String sTwo = "hallo ther
View Tutorial By: Adam at 2009-11-10 12:55:51
11. know someone how to compare various objects?
I would like to have only one comparator for obj
View Tutorial By: Peter at 2009-10-07 07:32:31
12. Superb
many thanks to you Mashoud. i'm trying to learn Java in CNAM France and this c
View Tutorial By: orchidouest at 2009-05-10 12:58:53
13. Waste code. doesnot work.Array index out of bounds exception
View Tutorial By: Tester at 2009-02-26 02:37:09
14. Hi you can find inheritance example in Java in the following tutorial. <br>
<a href=
View Tutorial By: Mashoud at 2008-04-26 20:12:37
15. how i can used inheritence ?
View Tutorial By: samee at 2008-02-16 12:54:27
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Java program to get location meta data from an image
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program