Meaningless Drivel is fun!*
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "String.compareTo()" Watch "String.compareTo()" New topic
Author

String.compareTo()

Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

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

Joined: Nov 04, 2001
Posts: 1871
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...
Regards
Maulin
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

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
 
Don't get me started about those stupid light bulbs.
 
subject: String.compareTo()