my dog learned polymorphism
The moose likes Java in General and the fly likes String.compareTo() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String.compareTo()" Watch "String.compareTo()" New topic


Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2591

Will comparteTo() method of the String class work with internationalization(diff locale)?
- Manish
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
Hi Manish

As far as I know compareTo() just do character by character comparision like,
int len1 = str1.length();
int len2 = str2.length();
if ( len1 != len2 )
return len1-len2;
int i=0;
while ( i < len2 ) {
if ( str1.charAt(i) != str2.charAt(i) )
return (int)str1.charAt(i) - (int)str2.charAt(i);
I guess....
Here I am not sure what would happen on two strings which are not having locale en but still same as the other...
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2591

Well, in compareToIgnoreCase method it is explicitly mentioned that -
Note that this method does not take locale into account, and will result in an unsatisfactory ordering for certain locales. The java.text package provides collators to allow locale-sensitive ordering.

So I was wondering if it is the same with compareTo as well.
- manish
I agree. Here's the link:
subject: String.compareTo()
It's not a secret anymore!