Example usage for java.text Collator compare

List of usage examples for java.text Collator compare

Introduction

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

Prototype

@Override
public int compare(Object o1, Object o2) 

Source Link

Document

Compares its two arguments for order.

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 {/* ww w.j a  va2  s.c  o  m*/
        System.out.println("Both Strings are not equal");
    }
}

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 {//from  w  ww.  ja  v  a  2  s .c  o  m
        System.out.println(str1 + "  and  " + str2 + "  are   the   same.");
    }
}

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  w w. ja  v  a2 s. c o  m
}

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  w w w.  j a  v a 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: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 . jav  a  2s  .  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();
}

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

public static void sortArray(Collator collator, String[] strArray) {
    String tmp;/*from   w  w w  .j av  a  2 s  . co  m*/
    if (strArray.length == 1)
        return;
    for (int i = 0; i < strArray.length; i++) {
        for (int j = i + 1; j < strArray.length; j++) {
            if (collator.compare(strArray[i], strArray[j]) > 0) {
                tmp = strArray[i];
                strArray[i] = strArray[j];
                strArray[j] = tmp;
            }
        }
    }
}

From source file:RulesDemo.java

public static void sortStrings(Collator collator, String[] words) {
    String tmp;//from   w  w  w.  j  a v  a  2 s . com
    for (int i = 0; i < words.length; i++) {
        for (int j = i + 1; j < words.length; j++) {
            // Compare elements of the words array
            if (collator.compare(words[i], words[j]) > 0) {
                // Swap words[i] and words[j]
                tmp = words[i];
                words[i] = words[j];
                words[j] = tmp;
            }
        }
    }
}

From source file:CollatorDemo.java

public static void sortStrings(Collator collator, String[] words) {
    String tmp;//from   w  w w. jav  a  2  s .  c  om
    for (int i = 0; i < words.length; i++) {
        for (int j = i + 1; j < words.length; j++) {
            // Compare elements of the array two at a time.
            if (collator.compare(words[i], words[j]) > 0) {
                // Swap words[i] and words[j]
                tmp = words[i];
                words[i] = words[j];
                words[j] = tmp;
            }
        }
    }
}

From source file:org.zephyrsoft.util.StringTools.java

/**
 * Compare two Strings with regards to the current default locale. If one of the Strings is {@code null}, it is
 * assumed "smaller" than the non-null String.
 * /*from w  w w  .j a  v a2  s.  co  m*/
 * @see String#compareTo(String)
 * @param one
 *            first parameter to compare
 * @param two
 *            second parameter to compare
 * @return the same scheme as in {@link String#compareTo(String)}
 */
public static int compareLocaleBasedWithNullFirst(String one, String two) {
    if (one == null && two != null) {
        return -1;
    } else if (one != null && two == null) {
        return 1;
    } else if (one == null && two == null) {
        return 0;
    } else if (one != null && two != null) {
        Collator collator = Collator.getInstance();
        return collator.compare(one, two);
    } else {
        // to silence the Eclipse warning, will never be executed:
        throw new IllegalStateException();
    }
}