prongs
prongs

Reputation: 9606

java internationalization rules

Java Internationalization Rules. It says to replace

s1.compareTo(s)==0

with

Collator.compare(s1,s2)<0

why <0?

Upvotes: 1

Views: 185

Answers (2)

P&#233;ter T&#246;r&#246;k
P&#233;ter T&#246;r&#246;k

Reputation: 116296

Based on the Javadoc, it is a typo - should be ==:

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.

This is for the String overload of Collator.compare (as the linked example compares Strings), but the general version of the method behaves the same way.

The article you referred indirectly links to the corresponding page of the Java Tutorial, which describes the behaviour consistently with the above.

Upvotes: 5

Michael Laffargue
Michael Laffargue

Reputation: 10304

I'm not sure you should rely on this site when I see the next point ... It creates a collator returning always 0.

package com.rule;
public class Do_not_use_String_compareToIgnoreCase_correction
{
    public void method()
    {
        new MyCollator().compare("String", "String"); // CORRECTION
    }

    class MyCollator extends java.text.Collator
    {
        public int compare(String source, String target)
        {
            return 0;
        }
        public java.text.CollationKey getCollationKey(String source)
        {
            return null;
        }
        public int hashCode()
        {
            return 0;
        }
    }
}

Upvotes: 0

Related Questions