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

    Detect Sort

    Zolotoj Community Member

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

       

      Thanks

        • 1. Re: Detect Sort
          DeanLoganBH Community Member

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

          • 2. Re: Detect Sort
            rtalton Community Member

            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

             

            where--

            "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:

             

            EXAMPLE

            //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;

            acMyDataProvider.refresh();

             

            //search for a record using the new sort:

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

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

            if(cursor.findFirst(searchObj)) {

                 trace('found');

                 //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;

            acMyDataProvider.refresh();

             

            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.

             

            HTH