Salomonsson.se
May 29 2012

Actionscript Vector Short Hand Notation

I can never remember the short hand notation for Vectors in actionscript 3, so I write it down here once and for all. Hopefully it might even help someone else.

You can declare and instanciate a new Vector by typing one of the following:

// Long way
var vec1:Vector.<int> = new Vector.<int>();
vec1.push(1);
vec1.push(2);
vec1.push(3);

// A little bit better
var vec2:Vector.<int> = Vector.<int>([1, 2, 3]);

// Yeah, I like this one B-)
var vec3:Vector.<int> = new <int>[1,2,3];

Another thing might worth noting is that even though the Vector-class is missing a sortOn()-method like array, and only provides a sort()-method (which accepts a reference to a comparison-method), you can actually pass in the same bitwise flags you could have used if doing a simple sort on an Array!

var vec:Vector..<int> = new <int>[5,1,3,11];
vec.sort(Array.NUMERIC|Array.DESCENDING);

trace(vec.join(",")); // outputs: 11,5,3,1