Archive

Posts Tagged ‘flex’

Sorting XMLListCollection and case sensitivity

February 23rd, 2009 Randy No comments

Consider this xml:

Actionscript:
  1. var myImageXML:XML =
  2.     <image>
  3.         <term text="New Term 498"/>
  4.         <term text="a new term 550"/>
  5.         <term text="a new term"/>
  6.         <term text="New Term 668"/>
  7.      </image>

You can create an xmlListCollection and then sort the collection based on the term text.

Actionscript:
  1. import mx.collections.SortField;
  2. import mx.collections.Sort;
  3.            
  4. var terms:XMLListCollection = new XMLListCollection(myImageXML..term);
  5. var sort:Sort = new Sort();
  6. var sortField:SortField = new SortField("@text");
  7. sort.fields = [sortField];
  8. terms.sort = sort;
  9. 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:
  1. sortField.caseInsensitive = true;

and the sort will happen as expected:

a new term
a new term 550
New Term 498
New Term 668

Categories: Flex development Tags: , , ,