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();}
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.
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
The code you have is correct, except for a type.
arrCol
should bear
. The code looks almost exactly like the code at the blog Flex Examples, which is also correct.Just change is change
arrCol
toar
like below:Not sure with numeric but otherwise everything else is correct.
Here is full example how to use sort in Array collection
http://blog.flexexamples.com/2007/08/05/sorting-an-arraycollection-using-the-sortfield-and-sort-classes/