Actionscript 3 如何在Flex中对ArrayCollection排序
我想按字段名对Arraycollection进行升序排序。这是我的密码,我想知道它是否正确。你有什么建议吗Actionscript 3 如何在Flex中对ArrayCollection排序,actionscript-3,apache-flex,flex3,static-data,Actionscript 3,Apache Flex,Flex3,Static Data,我想按字段名对Arraycollection进行升序排序。这是我的密码,我想知道它是否正确。你有什么建议吗 public static function arrayCollectionSort(ar:ArrayCollection, fieldName:String, isNumeric:Boolean):void {var dataSortField:SortField = new SortField(); dataSortField.name = fieldName
public static function arrayCollectionSort(ar:ArrayCollection, fieldName:String, isNumeric:Boolean):void
{var dataSortField:SortField = new SortField();
dataSortField.name = fieldName;
dataSortField.numeric = isNumeric;
var numericDataSort:Sort = new Sort();
numericDataSort.fields = [dataSortField];
arrCol.sort = numericDataSort;
arrCol.refresh();}
下面是如何在数组集合中使用排序的完整示例
除了类型之外,您的代码是正确的
arrCol
应为ar
。代码看起来几乎和博客上的代码一模一样,这也是正确的
只需将arrCol
更改为ar
,如下所示:
public static function arrayCollectionSort(ar:ArrayCollection, fieldName:String, isNumeric:Boolean):void
{
var dataSortField:SortField = new SortField();
dataSortField.name = fieldName;
dataSortField.numeric = isNumeric;
var numericDataSort:Sort = new Sort();
numericDataSort.fields = [dataSortField];
ar.sort = numericDataSort;
ar.refresh();
}
不确定是否使用数字,但其他所有内容都是正确的。您的代码很好,尽管如此,这里有几个示例,其中在单击按钮时应用了数字和字母排序 字母排序是对2个属性进行排序的一个很好的示例。在这种情况下,主要排序在“firstname”上完成,次要排序在“lastname”上完成 数字排序非常灵活,如果为排序字段的数字参数提供布尔值true,则排序会将属性强制转换为数字并按数字排序。如果提供的布尔值为false,则使用内置的字符串比较函数。在比较之前,每个数据项都转换为String()函数。默认值为null时,将对第一个数据项进行内省,以查看它是数字还是字符串,然后根据该内省进行排序
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" minWidth="955" minHeight="600">
<mx:Button label="Sort by first then last name" click="sortItemsByName()"/>
<mx:Button label="Sort by number" click="sortItemsByNumber()"/>
<mx:DataGrid dataProvider="{items}"
width="300"
height="300">
<mx:columns>
<mx:DataGridColumn dataField="number"/>
<mx:DataGridColumn dataField="firstname"/>
<mx:DataGridColumn dataField="lastname"/>
</mx:columns>
</mx:DataGrid>
<mx:ArrayCollection id="items">
<mx:Object number="3" firstname="John" lastname="Brown"/>
<mx:Object number="1" firstname="Kate" lastname="Brown"/>
<mx:Object number="4" firstname="Jeremy" lastname="Ryan"/>
<mx:Object number="5" firstname="Joe" lastname="Wilson"/>
<mx:Object number="2" firstname="Greg" lastname="Walling"/>
</mx:ArrayCollection>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.collections.Sort;
import mx.collections.SortField;
/**
* Sort the arraycollection by the firstname and then the last name
* */
private function sortItemsByName():void{
var srt:Sort = new Sort();
srt.fields = [new SortField("firstname"), new SortField("lastname")];
items.sort = srt;
items.refresh();
}
/**
* Sort the arraycollection numerically
* */
private function sortItemsByNumber():void{
var srt:Sort = new Sort();
srt.fields = [new SortField("number", true, false, true)];
items.sort = srt;
items.refresh();
}
]]>
</mx:Script>
</mx:Application>
这里还有sortField的语言参考
…以及数据提供程序和集合的Adobe livedocs参考
…这里有一个很好的livedocs参考,用于排序和筛选