Comparing Objects
The comparison operators (<, <=, ==, >=, >) are clearly core to any programming language. The AS3 docs tell us a little about AS3’s special handling of strings when compared, but there is more to the story.
As the aforementioned docs say, strings are compared character-by-character:
"hello" == ("h"+"ello"); // true "hello" == "goodbye"; // false "aaa" < "bbb"; // true "aaa" > "bbb"; // false "aaaa" < "aaa"; // false, length matters "aaaa" > "aaa"; // true, length mattering confirmed "aaaa" < "bbb"; // true, first comparison means length is not a tie-breaker "apple" > "aardvark"; // true
Most AS3 programmers know this language feature, which is not present in languages like Java and C++. Not many AS3 programmers know that the same comparison magic works with arrays and vectors. Well, almost:
[1,2,3] < [2,4,6]; // true [1,2,3] > [2,4,6]; // false [1,2,3] == [1,2,3]; // false Vector.<int>([1,2,3]) < Vector.<int>([2,4,6]); // true Vector.<int>([1,2,3]) > Vector.<int>([2,4,6]); // false Vector.<int>([1,2,3]) > Vector.<int>([1,2,3]); // false
Arrays and vectors are compared element-by-element. Less-than and greater-than operators work, but equality is still strict. You can see the language designer’s point here: you usually want to compare pointers rather than every element. Still, they could have made use of the === operator for that. So, now that you know all of that I never want to see any more manual loops; they’re just silly.
#1 by Hardik on March 15th, 2011 ·
Hello I am itermediate in as3. what is Vector. tht u write in ur example.
#2 by jackson on March 15th, 2011 ·
Vector
is likeArray
except it only holds one type, has a funky syntax, can’t have gaps between values, and can be limited to a certain length. Its purpose is to improve onArray's
performance.#3 by ricky on May 5th, 2011 ·
how about using below to test equality:
!([1,2,3] > [1,2,3] || [1,2,3] < [1,2,3]) //true
#4 by jackson on May 5th, 2011 ·
That statement makes sense to me. I see it breaking down this way:
This is because
[1,2,3]
is neither less than or greater to anArray
whose elements are identical to its own. Lastly, note that this is also true in JavaScript as well as ActionScript.