Hi Tracy,
  Thanks a lot for replying. That cleared a lot of my confusion between
XMLList and XMLListCollection. Based on your example I am able to sort
the XMLList. But there is one issue I observed. I have to reset my tree
dataProvider after sorting the XMLList (tree.dataProvider = booksList).
If I do not do that certain duplicate values start appearing in the tree
on multiple invocations of sort. I am not sure whats causing this as the
actual tree dataProvider (bookList) always remains in a consistent state
even after multiple invocations of the sort. My dataProvider is Bindable
so should not tree always represent the current state of the
dataProvider (bookList) without my resetting the dataProvider. Please
let me know if I am missing something.

  Following is my complete code:


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
layout="absolute">
     <mx:Script>
         <![CDATA[
             import mx.collections.XMLListCollection;
             import mx.collections.SortField;
             import mx.collections.Sort;
         private var books:XML = <books>
                         <book publisher="Addison-Wesley" name="Design
Patterns" />
                         <book publisher="Addison-Wesley" name="The
Pragmatic Programmer" />
                         <book publisher="Addison-Wesley" name="Test
Driven Development" />
                         <book publisher="Addison-Wesley"
name="Refactoring to Patterns" />
                         <book publisher="O'Reilly Media" name="The
Cathedral & the Bazaar" />
                         <book publisher="O'Reilly Media" name="Unit Test
Frameworks" />
                     </books>;
            [Bindable]
            private var booksList:XMLList = books.children();
            private function sortBooks():void
         {
             var sort:Sort = new Sort();
             sort.fields = [new  SortField("@name")];
             trace("**********************************Before
Sort************************************");
               trace(booksList);
             var booksCollection:XMLListCollection = new
XMLListCollection(booksList);
             booksCollection.sort = sort;
             booksCollection.refresh();
             var sortedCollection:XMLListCollection = new 
XMLListCollection();
               for(var i:Number=0; i < booksCollection.length; i++)
               {
                  
sortedCollection.addItemAt(booksCollection.getItemAt(i),i);
               }
               booksList = sortedCollection.source;
              //Commenting out the following line causes duplicate values
to appear in the tree
              tree.dataProvider = booksList;
               trace("**********************************After
Sort************************************");
               trace(booksList);
            }

         ]]>
     </mx:Script>
     <mx:VBox>
         <mx:Tree id="tree" dataProvider="{booksList}"
labelField="@name"/>
         <mx:Button click="sortBooks()" label="Sort books"/>
     </mx:VBox>
</mx:Application>




Regards,
Gaurav




--- In flexcoders@yahoogroups.com, "Tracy Spratt" <[EMAIL PROTECTED]> wrote:
>
> Ok.  Here is a function I use to sort the children of an xml node.
>
>
>
>     /** 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
>
>
>
> Tracy
>
>
>
> ________________________________
>
> From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED]
On
> Behalf Of Alex Harui
> Sent: Wednesday, July 30, 2008 5:58 PM
> To: flexcoders@yahoogroups.com
> Subject: RE: [flexcoders] Sorting an XMLList on an attribute
>
>
>
> Correct, they don't re-order the source since they are views of the
> source. FWIW, they actually create internal arrays of the sort
> order/filter function
>
>
>
> ________________________________
>
> From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED]
On
> Behalf Of Daniel Gold
> Sent: Wednesday, July 30, 2008 2:03 PM
> To: flexcoders@yahoogroups.com
> Subject: Re: [flexcoders] Sorting an XMLList on an attribute
>
>
>
> As I understand it, the collections APIs like XMLListCollection and
> ArrayCollection don't reorder their source lists at all, they just
> modify the way the lists are iterated over to return data in the
> requested order. So wrapping an Array in an ArrayCollection, adding a
> sort, and then trying to hand the sorted source Array off to someone
> else won't work, you have to wrap it, add sort, and then call
.toArray()
> on the collection to return a new Array containing the same elements
in
> sorted order.
>
> On Wed, Jul 30, 2008 at 3:22 PM, Alex Harui [EMAIL PROTECTED]
> <mailto:[EMAIL PROTECTED] > wrote:
>
> XMLList doesn't support Sort
>
>
>
> ________________________________
>
> From: flexcoders@yahoogroups.com <mailto:flexcoders@yahoogroups.com
> [mailto:flexcoders@yahoogroups.com <mailto:flexcoders@yahoogroups.com
]
> On Behalf Of gaurav1146
> Sent: Tuesday, July 29, 2008 11:58 PM
>
>
> To: flexcoders@yahoogroups.com <mailto:flexcoders@yahoogroups.com
> Subject: [flexcoders] Sorting an XMLList on an attribute
>
>
>
> Hi,
>
>
> I am trying to sort an XMLList based on one of the attributes in the
> XML node. On trying this I get the following error:
>
> TypeError: Error #1089: Assignment to lists with more than one item is
> not supported.
>
> Instead of XMLList if I use XMLListCollection it works fine. The thing
> is that I have used XMLList at lot of other places in my code as well
> so I cannot easily switch to XMListCollection. I would be prefer a way
> if I could sort the XMLList somwehow.
>
> Following is the sample code that I tried for the sorting difference
> between XMLList amd XMLListCollection:
>
> <?xml version="1.0" encoding="utf-8"?>
> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml
> <http://www.adobe.com/2006/mxml> "
> layout="absolute">
> <mx:Script>
> <![CDATA[
> import mx.collections.XMLListCollection;
> import mx.collections.SortField;
> import mx.collections.Sort;
> private var books:XML = <books>
> <book publisher="Addison-Wesley" name="Design
> Patterns" />
> <book publisher="Addison-Wesley" name="The
> Pragmatic Programmer" />
> <book publisher="Addison-Wesley" name="Test
> Driven Development" />
> <book publisher="Addison-Wesley"
> name="Refactoring to Patterns" />
> <book publisher="O'Reilly Media" name="The
> Cathedral & the Bazaar" />
> <book publisher="O'Reilly Media" name="Unit
> Test Frameworks" />
> </books>;
>
> // This does not work.
> /*
> [Bindable]
> private var booksList:XMLList = books.children();
> */
> [Bindable]
> private var booksList:XMLListCollection = new
> XMLListCollection(books.children());
>
>
> private function sortBooks():void
> {
> var s:Sort = new Sort();
> s.fields = [new SortField("@name")];
> booksList.sort = s;
> booksList.refresh();
> tree.dataProvider = booksList;
> }
>
> ]]>
> </mx:Script>
> <mx:VBox>
> <mx:Tree id="tree" dataProvider="{booksList}" labelField="@name"/>
> <mx:Button click="sortBooks()" label="Sort books"/>
> </mx:VBox>
> </mx:Application>
>

Reply via email to