1

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

1
  • so does the ordering of items matter? Commented Jul 27, 2010 at 8:41

4 Answers 4

1

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");
}       
Sign up to request clarification or add additional context in comments.

Comments

1
/* elements need to implement valueOf
public function valueOf():Object{}
*/
public static function equalsByValueOf(
    first:ArrayCollection, 
    seconde:ArrayCollection):Boolean{

    if((first==null) != (seconde==null) ){
        return false;
    }else if(!first && !seconde){
        return false;
    }

    if(first.length!=seconde.length){
        return false;
    }

    var commonLength:int = first.length;
    var dictionary:Dictionary = new Dictionary();            
    for(var i:int=0;i<commonLength;i++){
        var item1:Object = first.getItemAt(i);
        var item2:Object = seconde.getItemAt(i);
        dictionary[item1.valueOf()]=i;
        dictionary[item2.valueOf()]=i;
    }

    var count:int = 0;            
    for (var key:Object in dictionary)
    {
        count++;
    }
    return count==commonLength;
}


/* valueOf sample
 * something like javaObject.hashCode()
 * use non changing fields(recommended) 
 */

public function valueOf():Object{
 return "_"+nonChangeField1+"_"+nonChangeField2+"...";
}

Comments

0

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.

Comments

0

Dude, use the mx.utils.ObjectUtil... the creators of actionscript have already thought about this.

ObjectUtil.compare(collection1, collection2) == 0;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.