Previous Page
Next Page

Recipe 5.14. Comparing Arrays

Problem

You want to compare two arrays to see if they are equivalent.

Solution

Loop through each element of both arrays and compare them.

Discussion

Since arrays are reference datatypes, using an equality operator with two array variables only checks to see if they point to the same spot in memory. For example:

var letters:Array = ["a", "b", "c", "d"];
var lettersPointer:Array = letters;
trace(letters == lettersPointer);  // Displays: true

However, if two arrays are equivalent yet don't point to the same spot in memory, an equality operation returns false:

var letters1:Array = ["a", "b", "c", "d"];
var letters2:Array = ["a", "b", "c", "d"];
trace(letters1 == letters2];  // Displays: false

Instead, you can loop through each of the elements of the arrays and compare them:

var equivalent:Boolean = true;
for(var i:int = 0; i < letters1.length; i++) {
    if(letters1[i] != letters2[i]) {
        equivalent = false;
        break;
    }
}
trace(equivalent);  // Displays: true

Optionally, you can use the ArrayUtilities.equals( ) method. This method requires two parameters: the references to the two arrays to compare. The method returns a Boolean value that indicates whether the arrays are equivalent or not:

var letters1:Array = ["a", "b", "c", "d"];
var letters2:Array = ["a", "b", "c", "d"];
trace(ArrayUtilities.equals(letters1, letters2));
// Displays: true

By default, the order of the elements has to match in the two arrays. If you don't care whether the order of the elements matches, you can specify a third parameter for the equals( ) method; a Boolean value indicating whether or not the order should be disregarded:

var letters1:Array = ["a", "b", "c", "d"];
var letters2:Array = ["b", "a", "d", "c"];
trace(ArrayUtilities.equals(letters1, letters2));
// Displays: false
trace(ArrayUtilities.equals(letters1, letters2, true));
// Displays: true

The equals( ) method is fairly simple. The code is explained in the comments in the following code block:

public static function equals(arrayA:Array, 
                              arrayB:Array,
                              bNotOrdered:Boolean):Boolean {

    // If the two arrays don't have the same number of elements,
    // they obviously are not equivalent.
    if(arrayA.length != arrayB.length) {
        return false;
    }

    // Create a copy of each so that anything done to the copies 
    // doesn't affect the originals.
    var arrayACopy:Array = arrayA.concat(  );
    var arrayBCopy:Array = arrayB.concat(  );

    // If the order of the elements of the two arrays doesn't 
    // matter, sort the two copies so the order of the copies 
    // matches when comparing.
    if(bNotOrdered) {
        arrayACopy.sort(  );
        arrayBCopy.sort(  );
    }

    // Loop through each element of the arrays, and compare them. 
    // If they don't match, delete the copies and return false.
    for(var i:int = 0; i < arrayACopy.length; i++) {
        if(arrayACopy[i] != arrayBCopy[i]) {
            delete arrayACopy;
            delete arrayBCopy;
            return false;
        }
    }

    // Otherwise the arrays are equivalent. 
    // So delete the copies and return true.
    delete arrayACopy;
    delete arrayBCopy;
    return true;
}


Previous Page
Next Page
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)