2 Replies Latest reply on Apr 15, 2013 9:05 AM by rtalton

    Detect Sort

    Zolotoj Level 3

      Is it possible to detect an exisitng sort of a dataProvider (ArrayCollection)?



        • 1. Re: Detect Sort
          DeanLoganBH Level 3

          You could capture a sort on a DataGrid using sortChange and set a variable based on that change.

          • 2. Re: Detect Sort
            rtalton Level 4

            Hi Zolotoj,


            Within some method you have, you can get the DataProvider's sort like this:

            var currentSort:ISort = this.acMyDataProvider.sort


            Then you can easily examine the SortField's "name" property like this:

            var theSortFieldName:String = currentSort.fields[0].name



            "acMyDataProvider" is an ArrayCollection,

            "fields" is an Array of SortField,

            "name" is the name of the field used to sort the Collection.


            If more than one SortField is contained in the "fields" collection (like a multiple-field sort), you can loop through them to get the name of each SortField.

            Each SortField object contains all the information and options about the sort, like descending, numeric, compareFunction.




            If it helps anyone else, I often use the following technique to capture the current sort, re-sort on a different field, search for something using the new sort, then revert back to the original sort by doing this:



            //store the current sort, whatever it may be

            var currentSort:ISort = this.acMyDataProvider.sort;


            //change to another sort:

            var newSort:Sort = new Sort();

            newSort.fields = [new SortField('EmployeeName', false, false)];

            acMyDataProvider.sort = newSort;



            //search for a record using the new sort:

            var searchObj:Object = {EmployeeName:'Johnny'};

            var cursor:IViewCursor = this.acMyDataProvider.createCursor();

            if(cursor.findFirst(searchObj)) {


                 //do something with the record

                 var employee:Object = cursor.bookmark;

                 employee.ID = 95;

                 employee.HireDate = new Date();





            //revert back to the old sort, whatever it was:

            this.acMyDataProvider.sort = currentSort;



            In this case I don't need to know the name of the field(s) being sorted on; I just want to restore whatever sort the user has done (like in a DataGrid) after I search for something using a different sort.

            This should give you an idea on the different ways you can use sorting within a Collection.