kumar
kumar

Reputation:

DataGridColumn Sorting in ActionScript3

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

Upvotes: 0

Views: 543

Answers (2)

quoo
quoo

Reputation: 6307

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.

Upvotes: 0

CookieOfFortune
CookieOfFortune

Reputation: 13974

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

Upvotes: 2

Related Questions