11 Replies Latest reply: Mar 6, 2013 6:36 AM by Atul(Flex Dev) RSS

    multi column sorting

    Gboro54
      I am wondering if it is possbile to sort data grids in multi columns?? For example lets say the data grid simply looks like this:

      Name Date
      Art 10/29/06
      Chris 12/02/04
      Bob 02/21/07
      Bo 08/08/05

      And i want to sort it in abc order first then within that sort(abc order) have does be sorted again by date so that A's will stay together, B's and so on...Is this possible and if so how?

      Thanks for all the help
        • 1. Re: multi column sorting
          Gboro54 Community Member
          anyone have any suggestions for this...i could really use the help on this one...I am thinking about using list events to do this...is this on the right path at least???
          • 2. Re: multi column sorting
            Gboro54 Community Member
            does anyone have any suggestions on what to do for this??? Because i could really use the help,

            Thanks
            • 3. Re: multi column sorting
              raid01 Community Member
              var s:Sort = new Sort();
              // replace the field_name here
              s.fields = [ new SortField("Name",true),new SortField("Date",true)];
              your_data_grid.dataProvider.sort = s;
              your_data_grid.dataProvider.refresh();
              • 4. Re: multi column sorting
                Gboro54 Community Member
                i will try this and see if it works....
                • 5. Re: multi column sorting
                  Gboro54 Community Member
                  one more thing, the columns are being loaded in dynamically is it possible to parse the names into a string and then read the names of the columns into the sort???

                  Thanks
                  • 6. Re: multi column sorting
                    Gboro54 Community Member
                    anyone have any suggestions...The datagrid i am sorting has like 30 columns and when the user holds the ctrl key and clicks it should allow them to sort the grid but what ever columns they select...So how do i take the code given above by raid01 and allow it to set fields to true as the user clicks...( i know that i have to check to see if the ctrl key is pressed and things like that) any suggestions would be great...

                    Thanks again
                    • 7. Re: multi column sorting
                      Gboro54 Community Member
                      also mult column sorting should only happen when holding down the ctrl key...I am messing around with this but does anyone have any suggestions for how to handle the user clicking control and clicking on the columns at the same time
                      • 8. Re: multi column sorting
                        Gboro54 Community Member
                        alright i am having some trouble with this...the solution above works if i want the datagrid to be sorted by just those two columns...but lets say i have 6 columns and i want the user to click the columns that they want sorted(i.e. when the hold the ctrl key and click the mouse the columns the click are the ones that the datagrid is sorted by)...anyone have any suggestions???
                        • 9. Re: multi column sorting
                          Gboro54 Community Member
                          any suggestions???
                          • 10. Re: multi column sorting
                            Gboro54 Community Member
                            here is what i have...it crashes after the first click....

                            rivate function ctrlCheck(ke:KeyboardEvent):void // checks to see if the ctrl key is pressed to do a multicolumn sort.
                            {
                            if( ke.keyCode == 17) ctrlChecker =true;
                            else ctrlChecker = false;
                            }
                            /***************************************************************************************** ***********************/
                            private function resortColumns(le:ListEvent):void //if there is more than one page of results resort the columns and requery, else just resort
                            {
                            colSortedOn = new String("");
                            var sortDirection:String = new String("");
                            var s:Sort=new Sort();
                            var count:int=0;
                            var arr:Array=new Array();


                            if(le.rowIndex == 0)//get the column sorted on and attach that to the query and requery
                            {
                            if(ctrlChecker==true)
                            {
                            while(ctrlChecker==true)//while the user is still holding the ctrl key
                            {
                            colSortedOn = DataGridColumn(datagrid1.columns[le.columnIndex]).dataField.toString();
                            if(DataGridColumn(datagrid1.columns[le.columnIndex]).sortDescending)
                            {
                            sortLoadDirection = true;
                            }
                            else
                            {
                            sortLoadDirection = false;
                            }
                            arr[count]=new SortField(colSortedOn, true, sortLoadDirection)
                            count ++;
                            }
                            for(var i:int =0; i<arr.length; i++)
                            {
                            s.fields =arr
                            }
                            datagrid1.dataProvider.sort= s;
                            datagrid1.dataProvider.refresh();

                            }
                            }

                            else //there is no sort order taking place, the item click was something else entirely
                            {
                            parentDocument.sortCheck = 1;
                            }
                            }