Vector.sortOn()?



5 Comments

  • Share/Bookmark




As you probably know the new fp10 api provides a new Vector class (wonder my didn’t they just make typed Arrays like in C#) that increases performace about x2 times.
But, still among with the performance you could face some difficulties working with Vectors, for example it doesn’t implement a sortOn() method which, i think you’d agree, is very handy.

UPD: lots of people use very strange/complicated methods to sort vectors.
Guys, stop complicating yr life – the task is way much easier.

So, here’s how i do the sortOn() with Vectors:


/**
* Converts vector to an array
* @param    v:*        vector to be converted
* @return    Array    converted array
*/
function vectorToArray(v:*):Array
{
var n:int = v.length; var a:Array = new Array();
for(var i:int = 0; i < n; i++) a[i] = v[i];
return a;
}

/**
* Here's a little example on how to sort a Vector using sortOn
*/
var v:Vector.<Object> = new Vector.<Object>();
v.push({id:5, email:"andrew@someweb.com"});
v.push({id:35, email:"david@someweb.com"});
v.push({id:12, email:"jill@someweb.com"});

// now to sort the Vector on the "id" property
var vSorted:Vector.<Object> = Vector.<Object>(vectorToArray(v).sortOn("id", Array.NUMERIC));
// easy and convinient isn't it? ^_^

Hope might be useful.
Cheers