I18N Sort : Collator « I18N « Java






I18N Sort

I18N Sort
 
import java.text.Collator;
import java.text.ParseException;
import java.text.RuleBasedCollator;
import java.util.Collections;
import java.util.Locale;
import java.util.Vector;

public class SpanishSort {
  public SpanishSort() {
    Vector v = new Vector();
    v.add("musa");
    v.add("chic");
    v.add("llama");
    v.add("dela");
    v.add("chocolate");
    v.add("banana");
    v.add("crispa");
    v.add("luzca");

    Collator esCollator = Collator.getInstance(new Locale("es", "ES"));
    String spanishRules = ((RuleBasedCollator) esCollator).getRules();

    try {
      String traditionalRules = "& C < ch, cH, Ch, CH " + "& L < ll, lL, Ll, LL";
      RuleBasedCollator collate = new RuleBasedCollator(spanishRules + traditionalRules);

      Collections.sort(v, collate);
    } catch (ParseException e) {
      System.out.println("Error parsing rules " + e.toString());
    }

    StringBuffer result = new StringBuffer();
    for (int i = 0; i < v.size(); i++) {
      result.append(v.elementAt(i));
      result.append("\n");
    }
    System.out.println(result);
  }

  public static void main(String[] args) {
    new SpanishSort();
  }
}

           
         
  








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.Sort Collate
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