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.

 
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

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

  1. Charly says:

    Awesome…very cool. So simple and powerfull at once!!!

  2. Pierpaolo says:

    If I use this method, how can I use the sorted XMLlist in as3?

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree