在 Flex 中对 ArrayCollection 进行排序 [英] Sorting an ArrayCollection in Flex

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

问题描述

在 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.

谁能告诉我任何逻辑.

谢谢,库马尔

推荐答案

ArrayCollectionListCollectionView 的子类,具有 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆