views:

127

answers:

2

how to compare two arraycollection

 collectionArray1 = ({first: 'Dave', last: 'Matthews'},...........n values
 collectionArray = ({first: 'Dave', last: 'Matthews'},...........n values

how to compare..if equal just alert nochange if not alert chaged

A: 

I was going to say this.

if(collectionArray === collectionArray1) 

But that wont work (not triple = signs). As === is used to see classes.

  1. I would write a function called check if object exists in array.

  2. Create an array to hold elements that are not found. eg notFound

  3. in Collection1 go through all the element and see if they exist in Collection2, if an element does not exist, add it to the notFound array. Use the function your created in step1

  4. Now check Collection2, if an element is not found add it to the notFound array.

  5. There is no 5.

John Ballinger
+1  A: 

If you just want to know if they are different from each other, meaning by length, order or individual items, you can do the following, which first checks to see if the lengths are different, then checks to see if the individual elements are different. This isn't terribly reusable, it's left as an exercise for the reader to split this apart into cleaner chunks :)

public function foo(coll1:ArrayCollection, coll2:ArrayCollection):void {
    if (coll1.length == coll2.length) {
        for (var i:int = 0; i < coll1.length; i++) {
            if (coll1[i].first != coll2[i].first || coll1[i].last != coll2[i].last) {
                Alert.show("Different");
                return;
            }
        }
    }

    Alert.show("Same");
}       
Wade Mueller