Sort a Vector of Objects in ActionScript3

actionscript-3, object, sorting, vector

Solution

Your best bet for speed, access to Array's `sortOn()` and having a Vector as the result would be to just copy the content of the Vector across to an Array, use `sortOn()` and then copy the content back across. Example:

var vec:Vector.<Object> = new <Object>[
    { a: 2 }, { a: 1 }, { a: 12 }, { a: 7 }
];

var array:Array = [];
while(vec.length > 0) array.push(vec.pop());

array.sortOn("a", Array.NUMERIC|Array.DESCENDING);
while(array.length > 0) vec.push(array.pop());

for each(var i:Object in vec)
{
    trace(i.a);
}

Problem

I have this Vector of Objects and each Object has some properities(date, name, id, etc.). I want to sort the vector by, lets say, a date. How do I do this? I've seen, that an Array would support sortOn() function, but Vectors don't have it. Object: ``` public final class DisciplineEvent { public var id:Number; public var name:String; public var date:Date;} ``` Thanx for answering.

Original source