Reputation: 33
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.
Upvotes: 3
Views: 6158
Reputation: 39456
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);
}
Upvotes: 5
Reputation: 810
Let's say you have this vector:
var objects:Vector<ObjectType> = new Vector<ObjectType>();
objects.push(obj1, obj2);
You would sort it with:
var sortingFunction:Function = function(itemA:ObjectType, itemB:ObjectType):Number {
if (itemA.date.valueOf() < itemB.date.valueOf()) return -1; //ITEM A is before ITEM B
else if (itemA.date.valueOf() > itemB.date.valueOf()) return 1; //ITEM A is after ITEM B
else return 0; //ITEM A and ITEM B have same date
}
objects.sort(sortingFunction);
more information can be found here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Vector.html#sort()
Upvotes: 6