在 Flex 中对 ArrayCollection 进行排序 [英] Sorting an ArrayCollection in Flex
问题描述
在 Flex 中有什么方法可以让我们根据字符串对数组集合进行排序.
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.
谁能告诉我任何逻辑.
谢谢,库马尔
推荐答案
ArrayCollection
是 ListCollectionView
的子类,具有 sort
Sort
类有一个 compareFunction
property 可用于定义自定义排序函数.
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 进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!