I want to sort an Arraycollection by fieldName as ascending. Here's my code and I want to know whether it's right. Do you have any suggestions?
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();}
The code you have is correct, except for a type. arrCol
should be ar
. The code looks almost exactly like the code at the blog Flex Examples, which is also correct.
Just change is change arrCol
to ar
like below:
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();
}
Not sure with numeric but otherwise everything else is correct.
Here is full example how to use sort in Array collection
Your code is fine, even so here are a couple of examples where a numeric and an alphabetical sort is applied on button clicks.
The alphabetical sort is a good example of sorting on 2 attributes. In this case, the primary sort is done on the 'firstname', the secondary sort is done on the 'lastname'.
The numerical sort is quite flexible, if you provide a boolean value of true for the numeric parameter of the sort field, the sort will cast the attribute to a number and sort by number. If you provide a boolean value of false, the built-in string compare function is used. Each of data items is cast to a String() function before the comparison. With the default value of null, the first data item is introspected to see if it is a number or string and the sort proceeds based on that introspection.
<?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>
Also here is the language reference for the sortField...
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/collections/SortField.html
...and the Adobe livedocs reference for data providers and collections...
http://livedocs.adobe.com/flex/3/html/help.html?content=about_dataproviders_2.html
...and here is a good livedocs reference for sorting and filtering...
http://livedocs.adobe.com/flex/3/html/help.html?content=about_dataproviders_4.html
来源:https://stackoverflow.com/questions/9429142/how-to-sort-an-arraycollection-in-flex