Sorting an ArrayCollection in Flex

后端 未结 1 1400
Happy的楠姐
Happy的楠姐 2020-12-19 01:54

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\"

1条回答
  •  有刺的猬
    2020-12-19 02:18

    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):int
    {
       //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();
    

    0 讨论(0)
提交回复
热议问题