tags:

views:

381

answers:

1

Hi

Is there any way in Flex where in we can sort an arraycollection based on strings .

I have a dataprovider with strings like "Critical" "High" "Medium" "Low" where in I need to sort it in such a way that I need Critical to be displayed on the top and next High , Medium and Low follows.

Can somebody let me know any logic .

Thanks, Kumar

+2  A: 

ArrayCollection is a subclass of ListCollectionView that has a sort property. The Sort class has a compareFunction property that you can use to define custom sort functions.

private function sortFunction(a:Object, b:Object, array:Array = null):void
{
   //assuming that 'level' is the name of the variable in each object 
   //that holds values like "Critical", "High" etc
   var levels:Array = ["Low", "Medium", "High", "Critical"];
   var aLevel:Number = levels.indexOf(a.level);
   var bLevel:Number = levels.indexOf(b.level);
   if(aLevel == -1 || bLevel == -1)
      throw new Error("Invalid value for criticality ");
   if(aLevel == bLevel)
      return 0;
   if(aLevel > bLevel)
      return 1;
   return -1;
}
var sort:Sort = new Sort();
sort.compareFunction = sortFunction;
arrayCollection.sort = sort;
arrayCollection.refresh();
Amarghosh
It works good amar thanks
skumarvarma