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:Main.java

public static void main(String[] args) {
    Collator collator = Collator.getInstance(Locale.FRENCH);
    collator.setStrength(Collator.PRIMARY);

    if (collator.compare("d?b?rqu?r", "debarquer") == 0) {
        System.out.println("Both Strings are equal");
    } else {/*from   w w w  .j  a va  2  s .  c o  m*/
        System.out.println("Both Strings are not equal");
    }
}

From source file:Main.java

public static void main(String args[]) {

    String s1 = "";
    String s2 = "f";

    Collator frCollator = Collator.getInstance(Locale.FRANCE);
    frCollator.setStrength(Collator.CANONICAL_DECOMPOSITION);
    if (frCollator.compare(s1, s2) < 0) {
        System.out.println("s1 < s2");
    }//from   w ww  .  j av a 2s.c om
}

From source file:Main.java

public static void main(String[] args) {
    Locale USLocale = new Locale("en", "US");

    Collator c = Collator.getInstance(USLocale);
    String str1 = "Java";
    String str2 = "HTML";
    int diff = c.compare(str1, str2);
    System.out.print("Comparing using Collator  class: ");

    if (diff > 0) {
        System.out.println(str1 + "  comes after " + str2);
    } else if (diff < 0) {
        System.out.println(str1 + "  comes before " + str2);
    } else {/* w ww  . j  a  va 2 s .c  om*/
        System.out.println(str1 + "  and  " + str2 + "  are   the   same.");
    }
}

From source file:Main.java

public static void main(String[] args) {
    List<String> fruits = new ArrayList<String>();
    fruits.add("A");
    fruits.add("");
    fruits.add("C");
    fruits.add("D");
    fruits.add("A");

    Collator collator = Collator.getInstance(Locale.US);

    Collections.sort(fruits, collator);

    for (int i = 0; i < fruits.size(); i++) {
        String fruit = fruits.get(i);

        System.out.println("Fruit = " + fruit);
    }/*from w  w w.  ja va2 s  .  c o m*/
}

From source file:MainClass.java

public static void main(String[] args) {
    ArrayList list = new ArrayList();
    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);

    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i));
    }/*ww w .j ava 2s .  c  o m*/
}

From source file:CollateApp.java

public static void main(String args[]) {
    if (args.length != 1) {
        System.out.println("Usage: java CollateApp file");
        System.exit(0);/*from   www.j av a  2 s.com*/
    }
    Locale defaultLocale = Locale.getDefault();
    RuleBasedCollator collator = (RuleBasedCollator) Collator.getInstance(defaultLocale);
    Vector keyVector = new Vector();
    try {
        BufferedReader in = new BufferedReader(new FileReader(args[0]));
        String line;
        while ((line = in.readLine()) != null)
            keyVector.addElement(collator.getCollationKey(line));
        in.close();
    } catch (Exception ex) {
        System.out.println(ex);
        System.exit(0);
    }
    CollationKey keys[] = new CollationKey[keyVector.size()];
    for (int i = 0; i < keys.length; ++i)
        keys[i] = (CollationKey) keyVector.elementAt(i);
}

From source file:CollateApp.java

public static void main(String args[]) {
    if (args.length != 1) {
        System.out.println("Usage: java CollateApp file");
        System.exit(0);/*from ww  w .  j  ava  2 s . c  om*/
    }
    Locale defaultLocale = Locale.getDefault();
    RuleBasedCollator collator = (RuleBasedCollator) Collator.getInstance(defaultLocale);
    Vector<Object> keyVector = new Vector<Object>();
    try {
        BufferedReader in = new BufferedReader(new FileReader(args[0]));
        String line;
        while ((line = in.readLine()) != null)
            keyVector.addElement(collator.getCollationKey(line));
        in.close();
    } catch (Exception ex) {
        System.out.println(ex);
        System.exit(0);
    }
    CollationKey keys[] = new CollationKey[keyVector.size()];
    for (int i = 0; i < keys.length; ++i)
        keys[i] = (CollationKey) keyVector.elementAt(i);
}

From source file:org.eclipse.swt.snippets.Snippet2.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 2");
    shell.setLayout(new FillLayout());
    final Table table = new Table(shell, SWT.BORDER);
    table.setHeaderVisible(true);/*from ww  w  .  j  ava  2s  .c  o m*/
    final TableColumn column1 = new TableColumn(table, SWT.NONE);
    column1.setText("Column 1");
    final TableColumn column2 = new TableColumn(table, SWT.NONE);
    column2.setText("Column 2");
    TableItem item = new TableItem(table, SWT.NONE);
    item.setText(new String[] { "a", "3" });
    item = new TableItem(table, SWT.NONE);
    item.setText(new String[] { "b", "2" });
    item = new TableItem(table, SWT.NONE);
    item.setText(new String[] { "c", "1" });
    column1.setWidth(100);
    column2.setWidth(100);
    Listener sortListener = e -> {
        TableItem[] items = table.getItems();
        Collator collator = Collator.getInstance(Locale.getDefault());
        TableColumn column = (TableColumn) e.widget;
        int index = column == column1 ? 0 : 1;
        for (int i = 1; i < items.length; i++) {
            String value1 = items[i].getText(index);
            for (int j = 0; j < i; j++) {
                String value2 = items[j].getText(index);
                if (collator.compare(value1, value2) < 0) {
                    String[] values = { items[i].getText(0), items[i].getText(1) };
                    items[i].dispose();
                    TableItem item1 = new TableItem(table, SWT.NONE, j);
                    item1.setText(values);
                    items = table.getItems();
                    break;
                }
            }
        }
        table.setSortColumn(column);
    };
    column1.addListener(SWT.Selection, sortListener);
    column2.addListener(SWT.Selection, sortListener);
    table.setSortColumn(column1);
    table.setSortDirection(SWT.UP);
    shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:CollatorDemo.java

static public void main(String[] args) {

    testCompare();//www  . j a v  a  2s  .co m
    System.out.println();

    Collator fr_FRCollator = Collator.getInstance(new Locale("fr", "FR"));
    Collator en_USCollator = Collator.getInstance(new Locale("en", "US"));

    String eWithCircumflex = new String("\u00EA");
    String eWithAcute = new String("\u00E9");

    String peachfr = "p" + eWithAcute + "ch" + eWithAcute;
    String sinfr = "p" + eWithCircumflex + "che";

    String[] words = { peachfr, sinfr, "peach", "sin" };

    sortStrings(fr_FRCollator, words);
    System.out.println("Locale: fr_FR");
    printStrings(words);

    System.out.println();

    sortStrings(en_USCollator, words);
    System.out.println("Locale: en_US");
    printStrings(words);
}

From source file:TableSortByColumn.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Table table = new Table(shell, SWT.BORDER);
    table.setHeaderVisible(true);//from  w w  w.  j a v a  2  s .  co  m
    final TableColumn column1 = new TableColumn(table, SWT.NONE);
    column1.setText("Column 1");
    final TableColumn column2 = new TableColumn(table, SWT.NONE);
    column2.setText("Column 2");
    TableItem item = new TableItem(table, SWT.NONE);
    item.setText(new String[] { "a", "3" });
    item = new TableItem(table, SWT.NONE);
    item.setText(new String[] { "b", "2" });
    item = new TableItem(table, SWT.NONE);
    item.setText(new String[] { "c", "1" });
    column1.setWidth(100);
    column2.setWidth(100);
    Listener sortListener = new Listener() {
        public void handleEvent(Event e) {
            TableItem[] items = table.getItems();
            Collator collator = Collator.getInstance(Locale.getDefault());
            TableColumn column = (TableColumn) e.widget;
            int index = column == column1 ? 0 : 1;
            for (int i = 1; i < items.length; i++) {
                String value1 = items[i].getText(index);
                for (int j = 0; j < i; j++) {
                    String value2 = items[j].getText(index);
                    if (collator.compare(value1, value2) < 0) {
                        String[] values = { items[i].getText(0), items[i].getText(1) };
                        items[i].dispose();
                        TableItem item = new TableItem(table, SWT.NONE, j);
                        item.setText(values);
                        items = table.getItems();
                        break;
                    }
                }
            }
            table.setSortColumn(column);
        }
    };
    column1.addListener(SWT.Selection, sortListener);
    column2.addListener(SWT.Selection, sortListener);
    table.setSortColumn(column1);
    table.setSortDirection(SWT.UP);
    shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}