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 synchronized Collator getInstance() 

Source Link

Document

Gets the Collator for the current default locale.

Usage

From source file:Main.java

public static void main(String args[]) {

    String[] myArray = new String[] { "A", "B", "b" };
    Arrays.sort(myArray, Collator.getInstance());

    System.out.println(Arrays.toString(myArray));
}

From source file:SortWithCollationKeys.java

public static void main(String[] args) {
    Vector<String> list = new Vector<String>();
    list.add("m");
    list.add("c");
    list.add("e");
    list.add("c");

    Collator collate = Collator.getInstance();

    CollationKey[] keys = new CollationKey[list.size()];

    for (int k = 0; k < list.size(); k++)
        keys[k] = collate.getCollationKey((String) list.elementAt(k));

    CollationKey tmp;/*  w ww .  j a  v  a 2 s  . c  om*/
    for (int i = 0; i < keys.length; i++) {
        for (int j = i + 1; j < keys.length; j++) {
            if (keys[i].compareTo(keys[j]) > 0) {
                tmp = keys[i];
                keys[i] = keys[j];
                keys[j] = tmp;
            }
        }
    }
    for (int l = 0; l < keys.length; l++) {
        System.out.println(keys[l].getSourceString());
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    // Build a vector of words to be sorted
    ArrayList list = new ArrayList();
    list.add("m");
    list.add("c2");
    list.add("e");
    list.add("c1");

    Collator collate = Collator.getInstance();

    CollationKey[] keys = new CollationKey[list.size()];

    for (int k = 0; k < list.size(); k++)
        keys[k] = collate.getCollationKey((String) list.get(k));

    Arrays.sort(keys);/*from  w w  w  . j  a v  a2 s. com*/

    for (int l = 0; l < keys.length; l++) {
        System.out.println(keys[l].getSourceString());
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String[] words = { "", "e", "a", "c" };
    Writer w = new BufferedWriter(new OutputStreamWriter(System.out, "Cp850"));
    for (int i = 0; i < 4; i++) {
        w.write(words[i] + " ");
    }//w  ww . j a v  a2s. co m
    sortArray(Collator.getInstance(), words);
    for (int i = 0; i < 4; i++) {
        w.write(words[i] + " ");
    }
    w.flush();
    w.close();
}

From source file:Main.java

public static <K> SortedSet<K> newSortedSet() {
    return new TreeSet(Collator.getInstance());
}

From source file:Search.java

public static int indexOf(String source, String pattern) {
    RuleBasedCollator rbc = (RuleBasedCollator) Collator.getInstance();
    rbc.setStrength(Collator.SECONDARY);

    CollationElementIterator textCEI;
    CollationElementIterator patCEI;
    textCEI = rbc.getCollationElementIterator(source);
    patCEI = rbc.getCollationElementIterator(pattern);

    // e1 will contain the collation element for the source
    // e2 will contain the collation element for the pattern
    int e1, e2;/*from   w  ww .j  av  a  2  s . com*/
    int startMatch = -1;

    // initialize e2 with the first collation element in the pattern
    e2 = patCEI.next();

    while ((e1 = textCEI.next()) != CollationElementIterator.NULLORDER) {
        if (e1 == e2) { // if the elements match
            if (startMatch == -1)
                startMatch = textCEI.getOffset();
            e2 = patCEI.next(); // increment to the next element
            if (e2 == CollationElementIterator.NULLORDER)
                break;
        } else { // elements do not match
            if (startMatch != -1) {
                patCEI.reset();
                e2 = patCEI.next();
                startMatch = -1;
            }
        }
    }
    return startMatch;
}

From source file:Main.java

public static final int compareNatural(String firstString, String secondString) {
    int firstIndex = 0;
    int secondIndex = 0;

    Collator collator = Collator.getInstance();

    while (true) {
        if (firstIndex == firstString.length() && secondIndex == secondString.length()) {
            return 0;
        }//from w  ww  .  j  a  va2 s.  c  o  m
        if (firstIndex == firstString.length()) {
            return -1;
        }
        if (secondIndex == secondString.length()) {
            return 1;
        }

        if (Character.isDigit(firstString.charAt(firstIndex))
                && Character.isDigit(secondString.charAt(secondIndex))) {
            int firstZeroCount = 0;
            while (firstString.charAt(firstIndex) == '0') {
                firstZeroCount++;
                firstIndex++;
                if (firstIndex == firstString.length()) {
                    break;
                }
            }
            int secondZeroCount = 0;
            while (secondString.charAt(secondIndex) == '0') {
                secondZeroCount++;
                secondIndex++;
                if (secondIndex == secondString.length()) {
                    break;
                }
            }
            if ((firstIndex == firstString.length() || !Character.isDigit(firstString.charAt(firstIndex)))
                    && (secondIndex == secondString.length()
                            || !Character.isDigit(secondString.charAt(secondIndex)))) {
                continue;
            }
            if ((firstIndex == firstString.length() || !Character.isDigit(firstString.charAt(firstIndex)))
                    && !(secondIndex == secondString.length()
                            || !Character.isDigit(secondString.charAt(secondIndex)))) {
                return -1;
            }
            if ((secondIndex == secondString.length()
                    || !Character.isDigit(secondString.charAt(secondIndex)))) {
                return 1;
            }

            int diff = 0;
            do {
                if (diff == 0) {
                    diff = firstString.charAt(firstIndex) - secondString.charAt(secondIndex);
                }
                firstIndex++;
                secondIndex++;
                if (firstIndex == firstString.length() && secondIndex == secondString.length()) {
                    return diff != 0 ? diff : firstZeroCount - secondZeroCount;
                }
                if (firstIndex == firstString.length()) {
                    if (diff == 0) {
                        return -1;
                    }
                    return Character.isDigit(secondString.charAt(secondIndex)) ? -1 : diff;
                }
                if (secondIndex == secondString.length()) {
                    if (diff == 0) {
                        return 1;
                    }
                    return Character.isDigit(firstString.charAt(firstIndex)) ? 1 : diff;
                }
                if (!Character.isDigit(firstString.charAt(firstIndex))
                        && !Character.isDigit(secondString.charAt(secondIndex))) {
                    if (diff != 0) {
                        return diff;
                    }
                    break;
                }
                if (!Character.isDigit(firstString.charAt(firstIndex))) {
                    return -1;
                }
                if (!Character.isDigit(secondString.charAt(secondIndex))) {
                    return 1;
                }
            } while (true);
        } else {
            int aw = firstIndex;
            int bw = secondIndex;
            do {
                firstIndex++;
            } while (firstIndex < firstString.length() && !Character.isDigit(firstString.charAt(firstIndex)));
            do {
                secondIndex++;
            } while (secondIndex < secondString.length()
                    && !Character.isDigit(secondString.charAt(secondIndex)));

            String as = firstString.substring(aw, firstIndex);
            String bs = secondString.substring(bw, secondIndex);
            int subwordResult = collator.compare(as, bs);
            if (subwordResult != 0) {
                return subwordResult;
            }
        }
    }
}

From source file:IgnoreCaseComp.java

IgnoreCaseComp() {
    col = Collator.getInstance();

    col.setStrength(Collator.PRIMARY);
}

From source file:ro.nextreports.designer.util.TnsNameParser.java

public static List<String> getTnsDataSources(String path) {
    List<String> result = new ArrayList<String>();
    if (path == null) {
        return result;
    }/*from   w  w  w  .j  av a2s.  com*/
    StringBuffer fileData = new StringBuffer();
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader(new File(path, "tnsnames.ora")));
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            fileData.append(buf, 0, numRead);
        }

        Pattern pattern = Pattern.compile("[\\n][\\s]*([^\\(][\\w$#]+)[\\s]*=[\\s]*\\(");
        Matcher matcher = pattern.matcher(fileData.toString());
        while (matcher.find()) {
            result.add(matcher.group(1));
        }
        Collections.sort(result, new Comparator<String>() {
            public int compare(String s1, String s2) {
                return Collator.getInstance().compare(s1, s2);
            }
        });
    } catch (Exception ex) {
        ex.printStackTrace();
        LOG.error(ex.getMessage(), ex);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

From source file:org.apache.wiki.util.comparators.CollatorComparator.java

/**
 * Default constructor uses the current locale's collator.
 */
public CollatorComparator() {
    m_collator = Collator.getInstance();
}