Tuesday, May 3, 2011

Flex Fast ArrayCollection Search

I was working on Flex today and had to write fast Array Collection Search. here is code sample below:

        public function searchArrayCollection(usersCollection:ArrayCollection, sortField:String, searchObj:Object=null):Object
        {
            var foundObject:Object;
            var tempObject:Object;
            var mySort:Sort=new Sort();

            mySort.fields=[new SortField(sortField)];
            usersCollection.sort=mySort;
            usersCollection.refresh();

            var cursor:IViewCursor=usersCollection.createCursor();

            if (searchObj is String)
            { // this is just for string lookups
                tempObject=new Object();
                tempObject[sortField]=searchObj;

                searchObj=tempObject;
            }

            if (cursor.findAny(searchObj))
                foundObject=cursor.current;

            return foundObject;
        }

1 comment: