FileDocCategorySizeDatePackage
RulesDemo.javaAPI DocExample3019Tue Dec 12 18:57:36 GMT 2000None

RulesDemo

public class RulesDemo extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)


      String englishRules = 
         ("< a,A < b,B < c,C < d,D < e,E < f,F " +
          "< g,G < h,H < i,I < j,J < k,K < l,L " +
          "< m,M < n,N < o,O < p,P < q,Q < r,R " +
          "< s,S < t,T < u,U < v,V < w,W < x,X " +
          "< y,Y < z,Z");
  
      String smallnTilde  = new String("\u00F1");
      String capitalNTilde = new String("\u00D1");
  
      String traditionalSpanishRules = 
         ("< a,A < b,B < c,C " +
          "< ch, cH, Ch, CH "  +
          "< d,D < e,E < f,F " +
          "< g,G < h,H < i,I < j,J < k,K < l,L " +
          "< ll, lL, Ll, LL "  +
          "< m,M < n,N " +
          "< " + smallnTilde + "," + capitalNTilde + " " +
          "< o,O < p,P < q,Q < r,R " +
          "< s,S < t,T < u,U < v,V < w,W < x,X " +
          "< y,Y < z,Z");

       String [] words = {
         "luz",
         "curioso",
         "llama",
         "chalina"
       };

       try {
           RuleBasedCollator enCollator = 
              new RuleBasedCollator(englishRules);
           RuleBasedCollator spCollator = 
              new RuleBasedCollator(traditionalSpanishRules);
 
           sortStrings(enCollator, words);
           printStrings(words);
      
           System.out.println();
      
           sortStrings(spCollator, words);
           printStrings(words);
       } catch (ParseException pe) {
           System.out.println("Parse exception for rules");
       }
   
public static voidprintStrings(java.lang.String[] words)

       for (int i = 0; i < words.length; i++) {
          System.out.println(words[i]);
       }
   
public static voidsortStrings(java.text.Collator collator, java.lang.String[] words)

       String tmp;
       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;
               }
           }
       }