如何在GWT中比较locale String?

Sam*_*oth 5 java gwt

是的,因为GWT不支持java.text.Collat​​or(和java.util.Locale也); /

有解决方案吗

ken*_*ohn 5

我发现这个解决方案使用javascript.这是因为GWT不支持Collat​​or.

http://osdir.com/ml/GoogleWebToolkit/2009-06/msg01572.html

public class Collator {

   public static final Collator getInstance() {
      return instance;
   }

   private static final Collator instance = new Collator();

   public native int compare( String source, String target ); /*-{
     return source.localeCompare( target );
   }-*/
}
Run Code Online (Sandbox Code Playgroud)

我个人从未使用它,但它看起来很有希望.您可能必须进行更改,以便不存在跨浏览器问题.

编辑:
阅读JSNI.
这允许GWT在Java代码中调用原始的"javascript"代码.这就是我们在上面的课程中所做的."compare"方法对javascript进行本机调用.
http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html#writing

将Collat​​or.java添加到当前工作空间.

你应该能够比较如下.

Collator customCollator = Collator.getInstance();
if ( customCollator.compare( srcString , targetString ) > 0 )
{
     //the compare method will return an int.
     // write your code here.
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.