How to sort ArrayCollection in Flex

I want to sort Arraycollection by Name field as ascending. Here is my code and I want to know if this is correct. 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();} 
+6
source share
3 answers

The code you have is correct, except for the type. arrCol must be ar . The code looks about the same as the code in the Flex Examples blog, which is also correct.

Just change the value of arrCol to ar , as shown 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 what the number is, but otherwise everything is correct.

+16
source
+3
source

Your code is fine, even if these are a few examples where numeric and alphabetical sorts are applied at the click of a button.

Alphabetical sorting is a good example of sorting by 2 attributes. In this case, the primary sorting is performed on the "first", the secondary sorting is performed on the "last".

Numerical sorting is quite flexible, if you specify a boolean true for the numeric parameter of the sort field, the sort will pass the attribute to a number and sort by number. If you specify a boolean false, the built-in string comparison function is used. Before each comparison, each data element is passed to the String () function. When the default value is null, the first data item is checked for a number or string, and sorting continues based on this 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 a link to the sortField language ...

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/collections/SortField.html

... and the Adobe liveocs link for data providers and collections ...

http://livedocs.adobe.com/flex/3/html/help.html?content=about_dataproviders_2.html

... and here is a good link for sorting and filtering in liveocs ...

http://livedocs.adobe.com/flex/3/html/help.html?content=about_dataproviders_4.html

+3
source

Source: https://habr.com/ru/post/909265/


All Articles