I just created a test app and the sort DOES work with tags.  I pasted the
sample app below:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"; 
    layout="horizontal" horizontalAlign="left">
<mx:Script><![CDATA[
  import mx.collections.XMLListCollection;
  import mx.collections.Sort;
  import mx.collections.SortField;
  
  private function doSort():void
  {
    var xmlSorted:XML =
sortChildren(xmlToSort,tiSortField.text,cbxCase.selected,cbxDescending.selec
ted)
    txtSorted.text = xmlSorted.toXMLString();
  }  
    
    /** Sorts an xml node's children on a single attribute
    *   Returns a copy of the xml node with the children in the sorted order
*/
    public static function sortChildren(xml:XML, 
                                  sAttrName:String, 
                                  bCaseInsensitive:Boolean=true, 
                                  bDescending:Boolean=true):XML
    {
      var xmlReturn:XML;
      if (xml != null)  {
        xmlReturn = xml.copy();    //copy to get the root node.  We will
replace the children
        xml = xml.copy();                  //copy so we can append the
children to the return xml directly
        var xlcChildren:XMLListCollection = new
XMLListCollection(xml.children());
        var xlChildren:XMLList;
        var xlcSorted:XMLListCollection = new XMLListCollection();
        var sort:Sort = new Sort();        // Create the Sort instance.
        sort.fields = [new
SortField(sAttrName,bCaseInsensitive,bDescending)];  // Both fields are
case-insensitive.
        xlcChildren.sort = sort;          // Assign the Sort object to the
collection.
        xlcChildren.refresh();            // Apply the sort to the
collection.
        for (var i:int=0;i<xlcChildren.length;i++)  {      //loop over the
children in index order
          xlcSorted.addItemAt(xlcChildren.getItemAt(i),i); //add the node to
the new collection
        }
        xlChildren = xlcSorted.source;     //get the XMLlist from the source
property 
        xmlReturn.setChildren(xlChildren); //set the return xml children.

      }
      return xmlReturn;
    }//sortChildren      
]]></mx:Script>  

  <mx:TextArea id="txt1" width="300" height="300" text="{xmlToSort}"
editable="false" />
  <mx:VBox>
    <mx:CheckBox id="cbxCase" selected="true" label="Ignore case" />
     <mx:CheckBox id="cbxDescending" selected="false" label="Descending" />
    <mx:TextInput id="tiSortField" width="80" />
    <mx:Button label="SORT" click="doSort()" />
  </mx:VBox>
  <mx:TextArea id="txtSorted" width="300" height="300" editable="false" />  
  
  <mx:XML id="xmlToSort" xmlns="">
    <x>
      <y myAttribute="zzz">
        <a>
          hello
        </a>
        <b>
          world
        </b>
      </y>
      <y myAttribute="aaa">
        <a>
          apple
        </a>
        <b>
          pear
        </b>
      </y>
    </x>
  </mx:XML>
</mx:Application>

Tracy Spratt,
Lariat Services, development services available
________________________________________
From: [email protected] [mailto:[email protected]] On
Behalf Of Tracy Spratt
Sent: Saturday, September 26, 2009 10:48 AM
To: [email protected]
Subject: RE: [flexcoders] Sorting XML <HELP PLEASE>

  
And did you just try “a” as the sort field?  I would expect that to work. 
If you were sorting on an attribute named “a”, the sort field would be @a. 
I am pretty sure the sortField cannot handle expressions.  
 
Tracy Spratt,
Lariat Services, development services available
________________________________________
From: [email protected] [mailto:[email protected]] On
Behalf Of Tracy Spratt
Sent: Saturday, September 26, 2009 10:38 AM
To: [email protected]
Subject: RE: [flexcoders] Sorting XML <HELP PLEASE>
 
  
Use a custom sortFunction.  In that you will have references to the xml
nodes and you can use normal e4x expressions to traverse the child nodes,
then perform the sort logic.
 
Tracy Spratt,
Lariat Services, development services available
________________________________________
From: [email protected] [mailto:[email protected]] On
Behalf Of laurabakerstanley
Sent: Saturday, September 26, 2009 3:19 AM
To: [email protected]
Subject: [flexcoders] Sorting XML <HELP PLEASE>
 
  
Hi - 

I have structure that looks like this:
<x>
<y>
<a>
hello
</a>
<b>
world
</b>
</y>
<y>
<a>
apple
</a>
<b>
pear
</b>
</y>
</x>

I want to sort the 'records' i.e. those with a root of 'y' by one of the
tags in the their children.

I want to be able to create a sort field of y.a, but I cannot.

Does anybody know if I am banging my head against a brick wall here? Is
there away to seach against tage (I've seen lots of examples attributes) and
can I use 'complex field names' (with or without attributes)?

This stuff is all dynamic - I don't know what the structures might be, so
I'm really unkeen on using strong typing at all ....

I am doing this first thing on a saturday, as my deadline was yesterday, but
I might just be able to save my bacon if I can deliver for monday!

Thanks you for takig the time to read.

Laura


Reply via email to