Sorting Date’s on ArrayCollection in Flex

November 16, 2009

First things first, read about the mx.collections.Sort Class before going in to the code below.

About the Post,suppose i have a AC[ArrayCollection] like below;

[Bindable]
private var arrClldetails:ArrayCollection = new ArrayCollection([
{Fname:"Kranthi", Lname:"Kata", dob:"21 Sep 79"},
{Fname:"Vasanth", Lname:"Lola", dob:"11 Jun 80"},
{Fname:"Manoj", Lname:"Pati", dob:"16 July 81"},
{Fname:"John McClain", Lname:"Mela", dob:"15 Feb 72"},
{Fname:"Ross", Lname:"Geller", dob:"02 Jan 74"},
{Fname:"Chandler", Lname:"Bing", dob:"18 Oct 76"}
]);

Now i want to Sort the “dob” Date field in the AC.,so for this i need to
write a compareFunction for Sort which in turn is applied to the AC like below,

//Compare Function
private function fnCompareFunction(ObjA:Object,ObjB:Object,fields:Array = null):int
{
//fnDtfParceFunct is a Date parse function avail in DownloadCode.
var dateA:Date=fnDtfParceFunct(ObjA.dob,”DD/MM/YY”);
var dateB:Date=fnDtfParceFunct(ObjB.dob,”DD/MM/YY”);
return ObjectUtil.dateCompare(dateA, dateB);
}

Now apply this CompareFunction to Sort and then apply that to AC like below,

var sort:Sort=new Sort();
sort.compareFunction=fnCompareFunction;
arrClldetails.sort=sort;

Now refresh the AC for Sort to take effect,

arrClldetails.refresh();

Run Demo

Download Code

Enjoy the post.


Tracing Childrens of a Container in Flex

November 9, 2009

A Container in Flex is one that controls the layout characteristics of child components and which extends the Container base Class like the famous Panel,Canvas,ViewStack,TabNavigator,…

Container contains childrens or Sub Components or Sub Containers[Container in a Container], and so on.Childrens/child components in a Container are all are created using the creationPolicy setting of that Container.In case of sub containers, outer Containers are created before the inner one’s.And creation can be altered by setting the creationPolicy.

So now we know some basics of Container its time for the Topic.

Tracing..
We can manage the child components of a Container by using its predefined methods,like below..
1.Container.getChildren() – returns an Array of child components.
var arr:Array=Container.getChildren();
2.Container.getChildAt(index:int) – returns an instance of the DisplayObject at that creation index,which we can cast to required.
var obj:Object=Container.getChildAt(0);
3.Container.removeChildAt(index:int) – removes child at that creation index
var disObj:DisplayObject=Container.removeChildAt(0);
4.Container.removeAllChildren() – as the name specifies
….so on

In the below download code i have implemented all these methods with a Panel.Enjoy the Post.

Download code.