我有一个ArrayCollection中的项目列表,我想按某个值(不是按字母顺序排序)排序。有可能以这种方式排序吗?
以下是我想要订购的价值:
解决
封闭式
打开
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~
除了提供的答案
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~
如果您的数组集合是DataGrid的一部分,那么您可以在DataGrid的GridColumn上设置sortCompareFunction。请注意,此排序比较函数的语法不同,因为传入网格列而不是字段。
示例代码
与Spark DataGrid一起使用
private var desiredItemOrder:Array = ["In Progress", "Open", "Resolved", "Closed"];
private function sortCompareFunction(item:Object, anotherItem:Object, column:GridColumn = null):int
{
var itemIndex:Number = desiredItemOrder.indexOf(item.fields.status.name);
var anotherItemIndex:Number = desiredItemOrder.indexOf(anotherItem.fields.status.name);
if (itemIndex == -1 || anotherItemIndex == -1)
throw new Error("Invalid value for criticality ");
if (itemIndex == anotherItemIndex)
return 0;
if (itemIndex > anotherItemIndex)
return 1;
return -1;
}
<s:DataGrid dataProvider="{issuesCollection}">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="fields.status.name" headerText="Status" sortCompareFunction="sortCompareFunction"/>
</s:ArrayList>
</s:columns>
</s:DataGrid>
DOCUMENATION
Sort compareFunction
DataGridColumn sortCompareFunction
GridColumn sortCompareFunction
答案 0 :(得分:2)
这可能对你有所帮助
答案 1 :(得分:1)
如果你只想排序3个单词 - 在sort函数中进行这种比较,如:
private static var valuesOrder : Array = ["Resolved", "Closed", "Open"];
private function sortFunction(a:Object, b:Object, array:Array = null) : int{
var indexA : int = valuesOrder.indexOf(a);
var indexB : int = valuesOrder.indexOf(b);
return indexA - indexB;//return positive if indexA > indexB
}
var sort : Sort = new Sort();
sort.compareFunction = sortFunction;
arrayCollection.sort = sort;
arrayCollection.refresh();
注意,如果valuesOrder
数组中不存在值,则index将为-1。按照您的意愿处理这种情况。