The following example shows how you can sort an XML document by converting it into an XMLListCollection and applying a sort. You can also easily reverse the current sort by calling the SortField object’s reverse() method and refreshing the XMLListCollection object.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/12/04/sorting-xml-documents-using-an-xmllistcollection/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.utils.StringUtil;

            private function list_labelFunc(item:Object):String {
                return StringUtil.substitute("{0} {1} ({2} {3})",
                        item.@label, // city
                        item.@name, // name
                        item.parent().parent().@abbrev, // league
                        item.parent().@label); // division
            }

            private function checkBox_click(evt:MouseEvent):void {
                sortField.reverse();
                xmlListColl.refresh();
            }
        ]]>
    </mx:Script>

    <mx:XML id="mlb" source="mlb.xml" />

    <mx:XMLListCollection id="xmlListColl"
            source="{mlb.league.division.team}">
        <mx:sort>
            <mx:Sort>
                <mx:fields>
                    <mx:SortField id="sortField"
                            name="@label"
                            caseInsensitive="true" />
                </mx:fields>
            </mx:Sort>
        </mx:sort>
    </mx:XMLListCollection>

    <mx:ApplicationControlBar dock="true">
        <mx:CheckBox id="checkBox"
                label="reverse()"
                labelPlacement="left"
                click="checkBox_click(event);" />
    </mx:ApplicationControlBar>

    <mx:List id="list"
            dataProvider="{xmlListColl}"
            labelFunction="list_labelFunc"
            width="300"
            rowCount="8" />

</mx:Application>

View source is enabled in the following example.

 
Tagged with:
 
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.

2 Responses to Sorting XML documents using an XMLListCollection

  1. _Stef says:

    Hi,

    first, this blog is very very usefull ressource, thanks for us :).

    Second, sorry my question isn’t flex question (i haen’t see how contact you), i would to know what you use for insert the examples on the page. Plugn WP, self developpement ?

    I would make a tutorial on my blog and your insert solution is very nice.

    Thanks for all !!

  2. Jonathan says:

    Hi Peter, thank you for your precious post.
    I’ve a simple question and I’ll use this XML to explain it:

    AAA
    BBB

    111
    222

    I used succesfully your code (sorting by “node1″ or “node2″ ) but it doesn’t sort the XML by “node3.subnode3_1″

    Any ideas?

    Thanks in advance

    Jonathan