3 Replies Latest reply on Mar 12, 2008 7:55 AM by John Cambridge

    Sort an ArrayCollection of Objects

    John Cambridge
      I have an ArrayCollection where I'm storing Objects. These Objects have properties set like:
      var aurNode:Object = new Object();
      aurNode.categoryName = value.@name;
      aurNode.total = value.@totalSales;

      I want to sort this ArrayCollection by Object.total . How can I do this? I was looking through a page here: http://blog.flexexamples.com/2007/08/05/sorting-an-arraycollection-using-the-sortfield-and -sort-classes/ but I couldn't get it to work in my situation correctly. Does anyone have any suggestions?

      Thanks!
        • 1. Re: Sort an ArrayCollection of Objects
          Gregory Lafrance Level 6
          This seems to work:

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="init()">
          <mx:Script>
          <![CDATA[
          import mx.collections.SortField;
          import mx.collections.Sort;
          import mx.collections.ArrayCollection;

          [Bindable]
          private var ac:ArrayCollection = new ArrayCollection();

          private function init():void{
          var data:XML =
          <salesdata>
          <category name="drills" totalSales="100.00"/>
          <category name="hammers" totalSales="50.00"/>
          <category name="saws" totalSales="200.00"/>
          <category name="compressors" totalSales="300.00"/>
          <category name="paint" totalSales="25.00"/>
          </salesdata>;

          for (var cat:String in data.category)
          {
          var aurNode:Object = new Object();
          aurNode.categoryName = data.category.@name[cat];
          aurNode.total = data.category.@totalSales[cat];
          ac.addItem(aurNode);
          }
          }

          private function button_click():void {
          var dataSortField:SortField = new SortField();
          dataSortField.name = "total";
          dataSortField.numeric = true;

          var numericDataSort:Sort = new Sort();
          numericDataSort.fields = [dataSortField];

          ac.sort = numericDataSort;
          ac.refresh();

          }
          ]]>
          </mx:Script>

          <mx:List id="list" dataProvider="{ac}" textAlign="right" labelField="total" width="100" />
          <mx:Button id="button" label="sort items" click="button_click()" />
          </mx:Application>
          • 2. Re: Sort an ArrayCollection of Objects
            Garyl Woolworth Level 1
            I've written a test example of how you can add / remove a sort to an identifier on an array collection. Hope this helps.

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initComplete()">

            <mx:Script>
            <![CDATA[

            import mx.collections.Sort;
            import mx.collections.SortField;
            import mx.collections.ArrayCollection;

            private var sortage:Sort;

            [Bindable] private var phpData:ArrayCollection = new ArrayCollection();

            private function initComplete():void {
            for(var i:Number = 0; i < 20; i++){
            phpData.addItem({label:"item"+i, total:(i%2) + 100, isSelected:(i%2) ? true : false});
            }
            }

            private function sortCollection(collection:ArrayCollection, field:String, numeric:Boolean):void {
            // => Sort Collection
            sortage = new Sort();
            sortage.fields = [new SortField(field, false, false, numeric)];
            collection.sort = sortage;
            collection.refresh();
            }

            private function removeSort(collection:ArrayCollection):void {
            // => Remove Sort
            sortage = new Sort();
            sortage.fields = [new SortField(null, false, false, false)];
            collection.sort = sortage;
            collection.refresh();
            }

            ]]>
            </mx:Script>

            <mx:Button x="10" y="10" label="Create Sort" click="sortCollection(phpData, 'total', true)"/>
            <mx:Button x="120" y="10" label="Remove Sort" click="removeSort(phpData)"/>

            <mx:DataGrid left="10" right="10" y="40" dataProvider="{phpData}">
            <mx:columns>
            <mx:DataGridColumn headerText="Title" dataField="label"/>
            <mx:DataGridColumn headerText="Total" dataField="total"/>
            <mx:DataGridColumn headerText="Included" dataField="isSelected"/>
            </mx:columns>
            </mx:DataGrid>
            </mx:Application>
            • 3. Re: Sort an ArrayCollection of Objects
              John Cambridge Level 1
              Thanks a lot guys! It works perfectly!