Sort Collate : Collator « I18N « Java






Sort Collate

  
import java.io.BufferedReader;
import java.io.FileReader;
import java.text.CollationKey;
import java.text.Collator;
import java.text.RuleBasedCollator;
import java.util.Locale;
import java.util.Vector;

class CollateApp {
  public static void main(String args[]) {
    if (args.length != 1) {
      System.out.println("Usage: java CollateApp file");
    }
    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);
    }
    CollationKey keys[] = new CollationKey[keyVector.size()];
    keys = sort(keys);
    for (int i = 0; i < keys.length; ++i)
      System.out.println(keys[i].getSourceString());
  }

  static CollationKey[] sort(CollationKey keys[]) {
    boolean changes = true;
    while (changes) {
      changes = false;
      for (int i = 0; i < keys.length - 1; ++i) {
        int compare = keys[i].compareTo(keys[i + 1]);
        if (compare > 0) {
          changes = true;
          CollationKey temp = keys[i];
          keys[i] = keys[i + 1];
          keys[i + 1] = temp;
        }
      }
    }
    return keys;
  }
}

   
  








Related examples in the same category

1.Collator.getInstance(new Locale("sv", ""))
2.Comparing Half-Width and full-width A
3.Collator.NO_DECOMPOSITION
4.Collator.CANONICAL_DECOMPOSITION
5.Collator.FULL_DECOMPOSITION
6.I18N SortI18N Sort
7.Searching, Sorting, and Text Boundary Detection: Collation IssuesSearching, Sorting, and Text Boundary Detection: Collation Issues
8.Compare accentuated letters
9.Check Equality for two strings with Collator
10.Sort strings using Collator class
11.I18N : TextI18N : Text