Reputation: 833
Trying to use Collators for validating couple String objects., Here is the code.,
String myBubbleStr[] = {"zoon", "Hello", "World", "Yep", "Yow", "MyData"};
public void testCollatorStrings() {
Collator collator = Collator.getInstance();
String toCompare = "yow";
for (String collatorCompare : myBubbleStr) {
System.out.println(collator.compare(collatorCompare, toCompare));
}
}
My expectation of the output is -1,-1,-1,-1,1,-1. According to the documentation.
Returns an integer value. Value is less than zero if source is less than target, value is zero if source and target are equal, value is greater than zero if source is greater than target.
But the output I get is
1 -1 -1 -1 1 -1
Can somone help, how to get about this validation., The reason am using collators is to over the unicode restrictions for String comparison. Thanks.,
Upvotes: 2
Views: 2520
Reputation: 726809
The output looks correct, because collation order produced by the particular instance of Collator
considers the case of the letter only for tie-breaking.
zoon
is after yow
alphabetically, so the return value should be 1
Hello
is before yow
alphabetically, so the return value should be -1
World
is before yow
alphabetically, so the return value should be -1
Yep
is before yow
alphabetically, so the return value should be -1
Yow
is the same as yow
alphabetically, but starts in a capital letter, so the return value should be 1
MyData
is before yow
alphabetically, so the return value should be -1
You can lower collator's strength to make strings that differ only in case evaluate as identical.
Upvotes: 4