2 Replies Latest reply on Jul 26, 2007 2:23 PM by Gil1

    XMLListCollection Sorting

    ctzn99 Level 1
      Hello Once Again,

      Been grinding away and having huge amounts of success with flex. My client is happy and i'm mostly happy but i've been struggling with some sorting for a while now. It hasn't been critical as I figured out a way around what i'm doing but it's less than optimal. Basically, in the code i show below, i've got an XMLListCollection that i'm trying to sort, there are two lines, one that tries to sort based on an element in the list and another that tries to sort based on an attribute on the element in the list. The sorting by attribute works but of course, i need to actually sort by the element. Any thoughts or insight, as usual, would be greatly appreciated because as it stands now, i take the beautiful piece of xml returned from the server and end up parsing it into an array collection to have it do what i want.

      The crazy thing is, if you change the line:
      sorter.fields = [new SortFields("item", true);
      sorter.fields = [new SortFields("uncleBob", true);
      you get the same results.

      Here's my test application, you should be able to copy and past it directly into flex and see what I'm talking about

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
      import mx.collections.XMLListCollection;
      import mx.collections.SortField;
      import mx.collections.Sort;
      import mx.controls.Alert;

      private var xmlCollection:XMLListCollection;

      private var myData:XML =
      <item name="apples">Apples</item>
      <item name="oranges">Oranges</item>
      <item name="hand soap">Hand Soap</item>
      <item name="chocolate">Chocolate</item>
      <item name="brown sugar">Brown Sugar</item>
      <item name="cornFlakes">Corn Flakes</item>

      private function init():void
      xmlCollection = new XMLListCollection(myData.item);

      private function sort():void
      var sorter:Sort = new Sort();
      sorter.fields = [new SortField("item", true)]; // doesn't work.
      // sorter.fields = [new SortField("@name", true)]; // does work.
      xmlCollection.sort = sorter;

      private function debug():void

      <mx:List id="listData" x="276" y="191" dataProvider="{xmlCollection}" width="225"/>
      <mx:Button x="556" y="191" label="Sort" click="sort();"/>
      <mx:Button x="556" y="221" label="Debug" click="debug();"/>