13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Internationalizing applications<br />

Sorting and comparing strings<br />

Flash Player 10.1 and later, Adobe AIR 2.0 and later<br />

Collation is the process of arranging things in their proper order. Collation rules vary significantly by locale. The rules<br />

also differ if you are sorting a list or matching similar items, such as in a text search algorithm.<br />

Wh<strong>en</strong> sorting, small differ<strong>en</strong>ces such as upper and lowercase letters or diacritic marks such as acc<strong>en</strong>ts, are oft<strong>en</strong><br />

significant. For example, the letter ö (o with a diaeresis) is considered mostly equival<strong>en</strong>t to the plain letter o in Fr<strong>en</strong>ch<br />

or English. The same letter, however, follows the letter z in Swedish. Also, in Fr<strong>en</strong>ch and some other languages, the last<br />

acc<strong>en</strong>t differ<strong>en</strong>ce in a word determines its order in a sorted list.<br />

Wh<strong>en</strong> searching, you oft<strong>en</strong> want to ignore differ<strong>en</strong>ces in case or diacritics, to increase the chance of finding relevant<br />

matches. For example, a search for the characters “cote” in a Fr<strong>en</strong>ch docum<strong>en</strong>t conceivably returns matches for “cote”,<br />

“côte”, and “coté”.<br />

Using the Collator class<br />

The main methods of the Collator class are the compare() method, used primarily for sorting, and the equals() method,<br />

used for matching values.<br />

The following example shows the differ<strong>en</strong>t behavior of the compare() and equals() methods.<br />

var words:Array = new Array("coté", "côte");<br />

var sorter:Collator = new Collator("fr-FR", CollatorMode.SORTING);<br />

words.sort(sorter.compare);<br />

trace(words); // côte,coté<br />

var matcher:Collator = new Collator("fr-FR", CollatorMode.MATCHING);<br />

if (matcher.equals(words[0], words[1]))<br />

{<br />

trace(words[0] + " = " + words[1]); // côte = coté<br />

}<br />

The example first creates a Collator object in SORTING mode for the Fr<strong>en</strong>ch-France locale. Th<strong>en</strong> it sorts two words<br />

that differ only by diacritical marks. This shows that the SORTING comparison distinguishes betwe<strong>en</strong> acc<strong>en</strong>ted and<br />

non-acc<strong>en</strong>ted characters.<br />

The sorting is performed by passing a refer<strong>en</strong>ce to the Collator object’s sort() method as a parameter to the Array.sort()<br />

method. This technique is one of the most effici<strong>en</strong>t ways of using a Collator object to control sort order.<br />

The example th<strong>en</strong> creates a Collator object in MATCHING mode. Wh<strong>en</strong> that Collator object compares the two words, it<br />

treats them as equal. That shows that the MATCHING comparison values acc<strong>en</strong>ted and non-acc<strong>en</strong>ted characters the same.<br />

Customizing the behavior of the Collator class<br />

By default, the Collator class uses string comparison rules obtained from the operating system based on the locale and<br />

the user’s system prefer<strong>en</strong>ces. You can customize the behavior of the compare() and equals() methods by explicitly<br />

setting various properties. The following table lists the properties and the effect they have upon comparisons:<br />

Last updated 6/6/2012<br />

949

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!