Helpful Information
 
 
Category: Apache Flex
Flex arraycollection contains comparison

Hopefully flex has been around long enough that some of you flash guru's know how to use it.
I'm trying to get the index of an object inside an array collection, but the API information states that contains and getitemindex only return a result based on == of the provided object.
Given that the object does not appear to have an overridable equals method, and I can't find an IComparable interface, does anyone know if I can control what values the searching is performed on?
Given a 'Tag' object, which has a id, parent, desc, etc, and placed into an ArrayCollection, I want to get the tag associated with the tag id:


// This is in a graph object, also containing a get item index and get item at
public function contains(item:Tag):Boolean
{
return this.tags.contains(item);
}


The problem here is, unless the item has explicitly been defined, I can't get the contains result.
So, running something like:


// Graph has already had this tag added
var tag:Tag = new Tag();
tag.setID(1);
trace(graph.contains(tag)); // This returns false
tag.setParentID(-1);
tag.setDesc("No Desc");
tag.setTitle("No Title");
trace(graph.contains(tag)); // This returns true, now that all the fields have been added and are the same (assumably)

I want to convert my tag collection into a vertex collection, so I can easily add and remove neighbours from my adjacency graph. Since I won't know what the neighbour relationships will be, this contains method will not work.

Any ideas how to control the search behaviour of the contains/getitemindex values on the array collection? I was thinking of a compareTo, but like I said - no IComparable interface :(

Ok, looks like nobody has an idea what I'm talking about, I was hoping this is easier than it was.
Here is what I did, in case anybody else lands here from searching.
I created two new classes, one extending ArrayCollection, and another extending ArrayList. I also did an IComparable interface. I provided each of these with a delegate method for allowing comparisons to be done.
In the extended ArrayList class I overrided the getItemIndex, contains and getItemAt methods to handle the delegete methods for the comparisons.
I did the same in the ArrayCollections but chained them to the ArrayList methods. And finally, after the ArrayCollection has been instantiated, I set the deleget method to equal a comparison (like so):


myArrayCollection.compareFunction =
function compareTo(a:ICustomCompare, b:ICustomCompare):int
{
return a.compareTo(b);
}

Unfortunatly, I had not been able to get around a linear for loop while searching through the lists. Hopefully someone else has a better idea, but this does work - now I can implement my ICustomCompare interface on anything that requires comparisons in the lists.
Thanks for looking anyway guys, and, if you have a better solution, please post it, this was a lot of work for a simple comparison -_-










privacy (GDPR)