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

by Peter deHaan on August 15, 2007

in Sort, SortField, XML, XMLListCollection

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.

{ 2 comments… read them below or add one }

1 Charly August 16, 2007 at 2:13 am

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

Reply

2 Shawn Yale May 28, 2009 at 9:20 am

Perfect!

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: