1.21 gigawatts
1.21 gigawatts

Reputation: 17746

Filter and sort an ArrayCollection by another array

I have one array of about 10 items and an array collection of about 200 items. The array collection will contain the first 10 items somewhere in there.

I would like to filter the array collection and order it to only show the items in the first array and in the order the first array lists them in.

<s:ArrayCollection id="baseballCardCollection" >
    <fx:Object name="Bill"/>
    <fx:Object name="Jill"/>
    <fx:Object name="Phil"/>
    <fx:Object name="Luke"/>
    <fx:Object name="Duke"/>
    <fx:Object name="Zach"/>
    <fx:Object name="John"/>
    <fx:Object name="Don"/>
    <fx:Object name="Ron"/>
    <fx:Object name="Anne"/>
    <fx:Object name="Mark"/>
    <fx:Object name="Clark"/>
</s:ArrayCollection>

Filter and sort in order by:

<fx:Array>
  <fx:String>Zach</fx:String>
  <fx:String>Anne</fx:String>
  <fx:String>John</fx:String>
  <fx:String>Mark</fx:String>
  <fx:String>Luke</fx:String>
</fx:Array>

Goal:

<s:ArrayCollection id="baseballCardCollection" >
    <fx:Object name="Zach"/>
    <fx:Object name="Anne"/>
    <fx:Object name="John"/> 
    <fx:Object name="Mark"/>       
    <fx:Object name="Luke"/>        
</s:ArrayCollection>

Upvotes: 0

Views: 295

Answers (1)

Raja Jaganathan
Raja Jaganathan

Reputation: 36127

You need to apply filter function for arraycollection then put your logic into filter function. Item will be available in arraycollection if filter function return TRUE else item will removed from arraycollection but still avaliable in ArrayCollection.source property ie baseballCardCollection.source

 <fx:Script>
    <![CDATA[
        import mx.collections.Sort;
        import mx.collections.SortField;
        import mx.events.FlexEvent;

        protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
        {       
            fitlerByPersonArray();
            sortItemsByName();
        }

        private function fitlerByPersonArray():void {               
            baseballCardCollection.filterFunction = filterByPersonAndAddOrder;
            baseballCardCollection.refresh()
        }

        private function sortItemsByName():void{
            var srt:Sort = new Sort();
            var orderField:SortField = new SortField("order");
            orderField.numeric = true;

            srt.fields = [orderField]; //Order by 

            baseballCardCollection.sort = srt;
            baseballCardCollection.refresh();
        }           

        private function filterByPersonAndAddOrder(item:Object):Boolean
        {
            var index:int = personArray.indexOf(item.name);

            if(index > -1 ){
                item.order = index; //Create new property called Order then sort with the help of order property
                return true;
            }

            return false;                   
        }

    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
    <s:ArrayCollection id="baseballCardCollection" >
        <fx:Object name="Bill"/>
        <fx:Object name="Jill"/>
        <fx:Object name="Phil"/>
        <fx:Object name="Luke"/>
        <fx:Object name="Duke"/>
        <fx:Object name="Zach"/>
        <fx:Object name="John"/>
        <fx:Object name="Don"/>
        <fx:Object name="Ron"/>
        <fx:Object name="Anne"/>
        <fx:Object name="Mark"/>
        <fx:Object name="Clark"/>
    </s:ArrayCollection>

    <fx:Array id="personArray">
        <fx:String>Zach</fx:String>
        <fx:String>Anne</fx:String>
        <fx:String>John</fx:String>
        <fx:String>Mark</fx:String>
        <fx:String>Luke</fx:String>     
    </fx:Array>

</fx:Declarations>

<s:List dataProvider="{baseballCardCollection}" labelField="name" width="200" height="400">     
</s:List>

Upvotes: 1

Related Questions