Working with Text |
The previous section discussed how to perform string comparisons using the predefined rules for a locale. These collation rules determine the sort order of strings. If the predefined collation rules do not meet your needs, you can design your own rules and assign them to aRuleBasedCollator
object.Customized collation rules are contained in a
String
object which is passed to theRuleBasedCollator
constructor. Here's a simple example:For theString simpleRule = "< a < b < c < d"; RuleBasedCollator simpleCollator = new RuleBasedCollator(simpleRule);simpleCollator
object in the previous example, "a" is less than "b," which is less that "c," and so forth. ThesimpleCollator.compare
method references these rules when comparing strings. The full syntax used to construct a collation rule is more flexible and complex than this simple example. For a full description of the syntax, please refer to the API documentation for theRuleBasedCollator
class.The example that follows sortx a list of Spanish words with two collators. The full source code for this example is in the file called
RulesDemo.java
.The
RulesDemo
program starts by defining collation rules for English and Spanish. The program will sort the Spanish words in the traditional manner. When sorting by the traditional rules, the letters "ch," and "ll," and their upper case equivalents, each have their own position in the sort order. These character pairs compare as if they were one character. For example, "ch" sorts as a single letter, following "cz" in the sort order. Note how the rules for the two collators differ:The following lines of code creates the collators and invokes the sort routine: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");The sort routine, calledtry { 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"); }sortStrings
, is generic. It will sort any array of words according to the rules of anyCollator
object:When sorted with the English collation rules, the array of words is as follows:public static void sortStrings(Collator collator, 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; } } } }Compare the preceding list with the following, which is sorted according to traditional Spanish rules of collation:chalina curioso llama luzcurioso chalina luz llama
Working with Text |