2 Replies Latest reply on Sep 27, 2010 9:27 PM by bindu84

    sorting

    bindu84

      i want t o do multicolumn sort, at the time of initilixation of data grid

      where one column is type of  one is type of  Alphanumeric, one is type of date type.

       

       

      How to do that , i have attached some code but it is not working

       

       

      <?xml version="1.0" encoding="utf-8"?>

      <!-- http://flexonblog.wordpress.com/2009/11/16/sorting-dates-on-arraycollection-in-flex/ -->

      <mx:Application

       

       

      xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" initialize="init()">

       

       

      <mx:Script>

      <![CDATA[

       

      import mx.collections.SortField;

       

      import mx.controls.DateField;

       

      import mx.utils.ObjectUtil;

       

      import mx.collections.Sort;

       

      import mx.collections.ArrayCollection;

      [

      Bindable]

       

      private var arrClldetails:ArrayCollection = new ArrayCollection([

      {Fname:

      "Kranthi", Lname:"Kata", dob:"21/10/1972"},

      {Fname:

      "Vasanth", Lname:"Lola", dob:"12/01/1980"},

      {Fname:

      "Manoj", Lname:"Pati", dob:"16/09/1978"},

      {Fname:

      "John McClain", Lname:"Mela", dob:"15/01/1974"},

      ]);

       

      private function init():void

      {

       

      var sort:Sort=new Sort();

      sort.compareFunction=fnCompareFunction;

       

      var sortByName:SortField = new SortField("Fname");

       

      var sortByDate:SortField = new SortField("dob");

      sort.fields = [sortByDate,sortByName]

      arrClldetails.sort=sort;

      arrClldetails.refresh();

      }

       

       

       

       

       

       

      private function fnCompareFunction(ObjA:Object,ObjB:Object,fields:Array = null):int

      {

       

      var dateA:Date = new Date(Date.parse(ObjA.dob));

       

      var dateB:Date = new Date(Date.parse(ObjB.dob));

       

      return ObjectUtil.dateCompare(dateA, dateB);

       

      }

       

      ]]>

       

      </mx:Script>

       

      <mx:DateFormatter id="dateFormatter" formatString="DD/MM/YY"/>

       

      <mx:DataGrid id="dg" width="370" height="212" dataProvider="{arrClldetails}">

       

      <mx:columns>

       

      <mx:DataGridColumn headerText="First Name" dataField="Fname"/>

       

      <mx:DataGridColumn headerText="Last Name" dataField="Lname"/>

       

      <mx:DataGridColumn headerText="Date of Birth" dataField="dob"/>

       

      </mx:columns>

       

      </mx:DataGrid>

       

      </mx:Application>

        • 1. Re: sorting
          jfb00 Level 3

          Hi,

          Try this:

                     private function init():void
                      {
                          var sort:Sort=new Sort();
                         
                          var sortByName:SortField = new SortField();
                          sortByName.name = "Fname";
                         
                          var sortByDate:SortField = new SortField();
                          sortByDate.name = "dob";
                          sortByDate.compareFunction = fnCompareFunction;
                         
                          sort.fields = [sortByDate,sortByName]
                          arrClldetails.sort=sort;
                          arrClldetails.refresh();
                      }

           

          Johnny

          Please rate my answer. Tks!

          • 2. Re: sorting
            bindu84 Level 1

            Thanx a lot