Sorting XMLListCollection and case sensitivity
February 23rd, 2009
No comments
Consider this xml:
Actionscript:
-
var myImageXML:XML =
-
<image>
-
<term text="New Term 498"/>
-
<term text="a new term 550"/>
-
<term text="a new term"/>
-
<term text="New Term 668"/>
-
</image>
You can create an xmlListCollection and then sort the collection based on the term text.
Actionscript:
-
import mx.collections.SortField;
-
import mx.collections.Sort;
-
-
var terms:XMLListCollection = new XMLListCollection(myImageXML..term);
-
var sort:Sort = new Sort();
-
var sortField:SortField = new SortField("@text");
-
sort.fields = [sortField];
-
terms.sort = sort;
-
terms.refresh();
What threw me for a loop was the sort result (by term text value):
New Term 498
New Term 668
a new term
a new term 550
As it turns out the default value for the sort case insensitivity is false. To solve this, set the property in the SortField object:
Actionscript:
-
sortField.caseInsensitive = true;
and the sort will happen as expected:
a new term
a new term 550
New Term 498
New Term 668
