Example usage for java.text Collator getInstance

List of usage examples for java.text Collator getInstance

Introduction

In this page you can find the example usage for java.text Collator getInstance.

Prototype

public static Collator getInstance(Locale desiredLocale) 

Source Link

Document

Gets the Collator for the desired locale.

Usage

From source file:KeysDemo.java

static public void main(String[] args) {

    Collator enUSCollator = Collator.getInstance(new Locale("en", "US"));

    String[] words = { "peach", "apricot", "grape", "lemon" };

    CollationKey[] keys = new CollationKey[words.length];

    for (int k = 0; k < keys.length; k++) {
        keys[k] = enUSCollator.getCollationKey(words[k]);
    }//from   w  w w. jav a  2s  . c  o m

    sortArray(keys);
    displayWords(keys);
}

From source file:Main.java

public static void chineseSort(String[] strings) {
    Arrays.sort(strings, Collator.getInstance(java.util.Locale.CHINA));
}

From source file:Main.java

/**
 * Compare Chinese string// w  w  w .  j  a va 2s  .  c  om
 * @param chineseString1
 * @param chineseString2
 * @return
 */
public static int chineseCompare(String chineseString1, String chineseString2) {
    return Collator.getInstance(Locale.CHINESE).compare(chineseString1, chineseString2);
}

From source file:cz.afrosoft.whattoeat.core.gui.I18n.java

public static void init(final String language) {
    Locale locale = new Locale(language);
    I18n.resourceBundle = ResourceBundle.getBundle(TEXTS_BUNDLE_NAME, locale);
    I18n.collator = Collator.getInstance(locale);
}

From source file:JavaSort.java

public JavaSort() {
    Vector list = new Vector();
    list.add("\u00e4pple");
    list.add("banan");
    list.add("p\u00e4ron");
    list.add("orange");

    // Obtain a Swedish collator
    Collator collate = Collator.getInstance(new Locale("sv", ""));
    Collections.sort(list, collate);

    StringBuffer result = new StringBuffer();
    for (int i = 0; i < list.size(); i++) {
        result.append(list.elementAt(i));
        result.append(" ");
    }/*from w w w .  jav a 2  s .c o  m*/
    add(new JLabel(result.toString()));
}

From source file:CollatorDemo.java

public static void testCompare() {

    Collator myCollator = Collator.getInstance(new Locale("en", "US"));

    System.out.println(myCollator.compare("abc", "def"));
    System.out.println(myCollator.compare("rtf", "rtf"));
    System.out.println(myCollator.compare("xyz", "abc"));
}

From source file:com.github.rvesse.airline.utils.predicates.LocaleSensitiveStringFinder.java

public LocaleSensitiveStringFinder(String value, Locale locale) {
    if (locale == null)
        throw new NullPointerException("locale cannot be null");
    this.value = value;
    this.collator = Collator.getInstance(locale);
}

From source file:org.trnltk.apps.experiments.TurkishCollatorPerformanceTest.java

@Test
@Ignore//from   w ww .  j  ava  2  s . c  om
public void testCollatorPerformance() {
    final Collator collator = Collator.getInstance(Constants.TURKISH_LOCALE);
    collator.setStrength(Collator.PRIMARY);

    List<String> biggerList = getList();

    System.out.println("Entry count : " + biggerList.size());

    final StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    Collections.sort(biggerList);
    stopWatch.stop();
    System.out.println("w/o collator, it took " + stopWatch.toString());

    biggerList = getList();
    stopWatch.reset();
    stopWatch.start();
    Collections.sort(biggerList, collator);
    stopWatch.stop();
    System.out.println("w/ collator, it took " + stopWatch.toString());

}

From source file:Decomposition.java

public Decomposition() {
    String pairs[][] = new String[3][3];
    pairs[0][0] = "Half-Width and full-width A";
    pairs[0][1] = "A";
    pairs[0][2] = "\uFF21"; // full-width A
    pairs[1][0] = "A with Ring and Angstrom Sign";
    pairs[1][1] = "\u00c5"; // A with ring
    pairs[1][2] = "\u212b"; // Angstrom
    pairs[2][0] = "a + umlaut and precomposed a-umlaut";
    pairs[2][1] = "a\u0308";
    pairs[2][2] = "\u00e4";

    for (int i = 0; i < 3; i++) {
        Collator collate = Collator.getInstance(Locale.US);
        collate.setStrength(Collator.IDENTICAL);

        System.out.println("Comparing " + pairs[i][0]);
        collate.setDecomposition(Collator.NO_DECOMPOSITION);
        compare(collate, pairs[i][1], pairs[i][2]);

        collate.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
        compare(collate, pairs[i][1], pairs[i][2]);

        collate.setDecomposition(Collator.FULL_DECOMPOSITION);
        compare(collate, pairs[i][1], pairs[i][2]);
        System.out.println("");
    }/*w  ww . j av  a 2s.  co  m*/
}

From source file:org.nuxeo.ecm.platform.ui.web.directory.DirectorySelectItemComparator.java

public DirectorySelectItemComparator(String ordering, Boolean caseSensitive, Locale locale) {
    this.ordering = StringUtils.split(ordering, ",");
    this.caseSensitive = caseSensitive;
    if (locale == null) {
        FacesContext context = FacesContext.getCurrentInstance();
        this.locale = context.getViewRoot().getLocale();
    } else {/*from  www.java 2  s .c om*/
        this.locale = locale;
    }
    collator = Collator.getInstance(this.locale);
    if (Boolean.TRUE.equals(this.caseSensitive)) {
        collator.setStrength(Collator.TERTIARY); // TERTIARY will make a
                                                 // difference between 'a'
                                                 // and 'A'
    } else {
        collator.setStrength(Collator.SECONDARY);
    }
}