您的代码很好,即使这里有几个示例,在按钮单击时应用了数字和字母排序。
字母排序是对 2 个属性进行排序的一个很好的例子。在这种情况下,主要排序是在“名字”上完成的,次要排序是在“姓氏”上完成的。
数字排序非常灵活,如果你为排序字段的数字参数提供一个布尔值 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 的语言参考...
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/collections/SortField.html
...以及数据提供者和集合的 Adobe livedocs 参考...
http://livedocs.adobe.com/flex/3/html/help.html?content=about_dataproviders_2.html
...这是一个很好的 livedocs 参考,用于排序和过滤...
http://livedocs.adobe.com/flex/3/html/help.html?content=about_dataproviders_4.html