views:

109

answers:

2

Hi ,

Can somebody let me know whther we can sort this type of data in FLEX

DATA --

1ST QUARTER 2007 2ND QUARTER 2006 2ND QUARTER 2007 2ND QUARTER 2006 When i sort i need something like this ..

1ST QUARTER 2006 2ND QUARTER 2006 1ST QUARTER 2007 2ND QUARTER 2007

This is part of DataGridColumn Sorting when i apply default sort iam getting like

1st quarter 2006 1st quarter 2007 2nd quarter 2006 2nd quarter 2007

Can some body let me know whether you have logic or You have done something like this earlier .

Thanks, Kumar

A: 

You'll want to use a custom sort function, ie:

<mx:DataGridColumn dataField="quarter" headerText="Quarter" width="100"
 sortCompareFunction="sortQuarter"/>

public function sortQuarter(obj1:Object, obj2:Object):int{
   //where obj1 and obj2 are your data objects containing the quarter strings, you'll need to parse them to compare to see which one is greater.
   if(obj1 < obj2){
      return -1;
   }

   if(obj1 == obj2){
      return 0;
   }


   if(obj1 > obj2){
      return 1;
   }
}

I'd suggest just breaking off the last 4 chars of the quarter strings to compare the years, and then if they're the same, compare the first chars of each string.

quoo
Hi,Do I need to take numericals in one object and strings in one objec to compare
kumar
Sorry if I wasn't clear: obj1 and obj2 will be the data objects that populates the grid. You'll need to pull the strings out of them. See: http://livedocs.adobe.com/flex/3/langref/mx/controls/dataGridClasses/DataGridColumn.html#sortCompareFunction. You'll then need to figure out a way to compare the data in each of these objects - maybe a hidden year and quarter variable in the data object would make this easier.
quoo
+2  A: 

Are they strings...? You can define your own sorting function like this: sortCompareFunction

dataGridColumn.sortCompareFunction = compareQuarters;

private function compareQuarters(lhs:Object, rhs:Object):int
{
    var lhsArray:Array = lhs.split(" ");
    var rhsArray:Array = rhs.split(" ");
    if(lhsArray[2] > rhsArray[2])
    {
        return -1;
    }
    if(lhsArray[2] < rhsArray[2])
    {
        return 1;
    }
    if(lhsArray[0] > rhsArray[0])
    {
        return -1;
    }
    if(lhsArray[0] < rhsArray[0])
    {
        return 1;
    }
    return 0;
}
CookieOfFortune
Hi Tried to add the above logic but the 1st and 2nd things are not getting sorted
kumar
HI ,ITS WORKING COOL , IT WAS MY MISTAKE THAT I HAVE WRITTEN WRONG LOGIC .THANKS FOR THE HELP
kumar