Sorting an XMLListCollection using the mx:Sort and mx:SortField MXML tags

I actually searched around and couldn’t find any other examples of this, but here is a quick example of using the <mx:Sort /> and <mx:SortField /> MXML tags in Flex to sort an XMLListCollection.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/15/sorting-an-xmllistcollection-using-the-mxsort-and-mxsortfield-mxml-tags/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:XML id="myXML" source="http://feeds.feedburner.com/FlexExamples?format=xml" format="e4x" />
    <mx:XMLListCollection id="xlc" source="{myXML..item.category}" sort="{mySort}" />

    <mx:Sort id="mySort">
        <mx:fields>
            <mx:SortField name="*" caseInsensitive="true" />
        </mx:fields>
    </mx:Sort>

    <mx:VBox horizontalAlign="left">
        <mx:Label text="Categories ({xlc.length}):" />
        <mx:List id="list"
                dataProvider="{xlc}"
                rowCount="5"
                width="200"
                creationComplete="{xlc.refresh()}" />
    </mx:VBox>

</mx:Application>

View source is enabled in the following example.

3 thoughts on “Sorting an XMLListCollection using the mx:Sort and mx:SortField MXML tags

Comments are closed.