Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort a Vector of Objects in ActionScript3

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.

like image 381
Marek Mensik Avatar asked Aug 15 '26 06:08

Marek Mensik


1 Answers

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()

like image 115
Marijn Avatar answered Aug 17 '26 23:08

Marijn