5 Replies Latest reply on Dec 13, 2010 8:20 AM by nikos101

    Sorting in Grouping Collection

    SOHIAD

      We are using mx:GroupingCollection to group data in mx:AdvanceddataGrid in flex and are able to group data

      based on some field value, but we are unable to get a default sort order under groupings when the advanced datagrid gets

      rendered for the first time,However  we are able to sort the data by clicking the grid header.

      Can any one help us as to how to get sorted under groupings for the first time ?

        • 1. Re: Sorting in Grouping Collection
          =VA=FyreHeart Level 2

          Sure.

           

          You need to specify a callback function in (one of) your GroupingFields. e.g.

          <mx:GroupingField name="whatever" compareFunction="mySortFunction">

           

          The compare function then needs to return -1 (item1 is before item2), 0 (item1==item2), or 1 (itm1 is after item2) as the return value. Something like this:

           

          private function mySortFunction(item1:Object, item2:Object):int {

               if(item1.fieldName > item2.fieldName)

                    return 1;

               else

                    return -1;

          }

          • 2. Re: Sorting in Grouping Collection
            SOHIAD Level 1

            I tried this but when I use "mySortFuntion" in mx:GroupingField my data are getting sorted in a wired manner.

            My groups are getting displayed multipule times.

            My data is getting sorted but values for a particular group is not coming in one set, its coming in diffrent sets and the group name is getting repeated every time .

            • 3. Re: Sorting in Grouping Collection
              Sameer Bhatt Adobe Employee

              You said that you are able to sort the data by clicking the grid header and that you want the same sorting order.

              To do this, you can apply sorting to the collection after grouping -

               

              // apply grouping and wait for the grid to render the grouped data
              gc.refresh();
              adg.dataProvider = gc;
              adg.validateNow();
              
              // apply sort to the dataProvider
              var coll:ICollectionView = adg.dataProvider as ICollectionView;
              var sort:Sort = new Sort();
              sort.fields = [new SortField("FIELD_NAME")];
              coll.sort = sort;
              coll.refresh();
              
              • 4. Re: Sorting in Grouping Collection
                =VA=FyreHeart Level 2

                It sounds like you need to sort across multiple fields. If that's so, you'll need to concatenate the fields and then apply your sorting logic. If you don't concat the fields first, you'll get all kinds of weird stuff happening. I've had to do that for a custom sort in one app (the sort is neither Alphabetical or Numerical.

                 

                So, your function would look more like this:

                private function mySortFunction(item1:Object, item2:Object):int {

                     var _concatItem1:String = item1.field1+' '+item1.field2;

                     var _concatItem2:String = item2.field1+' '+item1.field2;

                 

                     switch(_concatItem1) {

                          case 'value1 value2':

                               switch(_concatItem2) {

                                    case 'value1 value2':

                                         return 0;

                                         break;

                                    case 'value3 value4':

                                    case 'value5 value6':

                                         return -1;

                                         break;

                                    default:

                                         return 1;

                               }

                               break;

                          case 'value3 value4':

                               switch(_concatItem2) {

                                    case 'value3 value4':

                                         return 0;

                                         break;

                                    case 'value1 value2':

                                         return 1;

                                         break;

                                    default:

                                         return -1;

                               }

                               break;

                          default:

                               return 1;

                     }

                }

                 

                It's tedious, but it does work.

                 

                If you don't need a custom sort, just concat the fields and do your normal if > logic as before.

                • 5. Re: Sorting in Grouping Collection
                  nikos101 Level 2

                  yikes thats a nasty hack