FileDocCategorySizeDatePackage
SpanishSort.javaAPI DocExample1082Fri Mar 30 00:15:36 BST 2001None

SpanishSort.java

import java.text.*;
import java.util.*;

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();
  }
}