本文介绍了在Flex中排序ArrayCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个类似CriticalHigh的数据提供者Medium 低
在哪里我需要排序的方式,我需要关键将显示在上面和下一个高,中,低如下。



谢谢,
Kumar

解决ArrayCollection 是 ListCollectionView 的一个子类,它有一个 sort 。 Sort 类有一个 compareFunction 来定义自定义排序功能。



<$ p $ (

$ b //假设'level'是变量的名字在每个对象
//中保存Critical,High等值
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错误(Critical value for criticality);
if(aLevel == bLevel)
return 0;
if(aLevel> bLevel)
return 1;
返回-1;

var sort:Sort = new Sort();
sort.compareFunction = sortFunction;
arrayCollection.sort = sort;
arrayCollection.refresh();


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

解决方案

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();

这篇关于在Flex中排序ArrayCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:13