Vector.sortOn()?



10 Comments

Share




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

10 Comments (+add yours?)

  1. JTtheGeek
    Mar 16, 2010 @ 14:36:18

    This seems like a pretty rough way to go since your unboxing then reboxing the typed vector to an untyped array then back to a typed vector. Though very simple and effective for large datasets I’m guessing the performance is gonna take a pretty big hit, but depends on how Adobe implemented the vector to array conversion. Would be great to get some performance comparisons there. Thanks for the tip.

  2. jloa
    Mar 16, 2010 @ 16:08:00

    U r welcome :)
    Well, yeah the method is not very “politically clear” if u know what i mean, BUT still it’s very simple and clear as u save the “sortOn” construction and its native params. Yeap, i’ll compare the performance and publish relsults here as soon as i rake up my current project’s stuff ^_^

    Btw it seems even more handy if done as a static Util class:

    
    package
    {
    	public class VectorUtil
    	{
    		/**
    	 	* Converts vector to an array
    		* @param	vector:*	vector to be converted
    	 	* @return	Array		converted array
    	 	*/
    		public static function vectorToArray(vector:*):Array
    		{
    			var n:int = vector.length; var a:Array = new Array();
    			for(var i:int = 0; i < n; i++) a[i] = vector[i];
    			return a;
    		}
    		/**
    		 * Converts vector to an array and sorts it by a certain fieldName, options
    		 * for more info @see Array.sortOn
    		 * @param	vector:*			the source vector
    		 * @param	fieldName:Object	a string that identifies a field to be used as the sort value
    		 * @param	options:Object		one or more numbers or names of defined constants
    		 */
    		public static function sortOn(vector:*, fieldName:Object, options:Object  = null):Array
    		{
    			return vectorToArray(vector).sortOn(fieldName, options);
    		}
    	}
    }
    
    

    Then the usage would be even clear:

    
    //...
    var vSorted:Vector.<Object> = Vector.<Object>(VectorUtil.sortOn(v, "id", Array.NUMERIC));
    
    

  3. maverxkzZ
    Apr 09, 2010 @ 23:17:24

    Not bad article, but I really miss that you didn’t express your opinion, but ok you just have different approach

  4. jloa
    Apr 24, 2010 @ 08:36:13

    Well, i do have my own opinion.
    To clarify, my opinion is that you always have to be practical, despite being a developer. If you are making a small project (micro web site, banner eg) you do not have to spend days optimizing every bit of your source code to get a 100 miliseconds increase in performance. As that could cause unprofitability of your project, as “every project has its own solution”.
    You don’t use Oracle databases for your forum, do you? :)

    In this case you get a nice, clear method which is very handy and lightweight. And it doesn’t claim to be extra-fast. Of course i wouldn’t use this method in a 3d engine for massive vectors sorting etc before checking its performance.

  5. dendy
    Aug 06, 2010 @ 02:12:19

    thanks for interesting post!

  6. John Grden
    Nov 09, 2010 @ 07:15:53

    Worked perfectly – easy to implement ;)

    Thanks man,

    John

  7. jloa
    Nov 09, 2010 @ 08:24:20

    You are welcome :)
    Nice to know that smbd find it useful

  8. Wil
    Mar 21, 2011 @ 10:01:50

    why not just cast the vector as an array:

    Array(Vector.).sortOn

  9. jloa
    Mar 21, 2011 @ 16:11:42

    well, try it to figure out why ^_^

  10. Christian
    Jan 16, 2013 @ 17:23:33

    Even more easy and convenient:

    var v:Vector. = new Vector.();
    v.push({id:5, email:”andrew@someweb.com”});
    v.push({id:35, email:”david@someweb.com”});
    v.push({id:12, email:”jill@someweb.com”});

    var vSorted:Vector. = Vector.((v as Array).sortOn(“id”, Array.NUMERIC));

    btw, Array(Vector) doesnt work because Array(…) works the same as new Array(…). It’ll try to make an Array with the Vector as an element, not cast the Vector to an Array. I don’t know why, but thats how it is. Also, instead of this:

    var v:Vector. = new Vector.();
    v.push({id:5, email:”andrew@someweb.com”});
    v.push({id:35, email:”david@someweb.com”});
    v.push({id:12, email:”jill@someweb.com”});

    you could do this:

    var v:Vector. = new [
    {id:5, email:"andrew@someweb.com"},
    {id:35, email:"david@someweb.com"},
    {id:12, email:"jill@someweb.com"}
    ];

    so then you have:

    var v:Vector. = new [
    {id:5, email:"andrew@someweb.com"},
    {id:35, email:"david@someweb.com"},
    {id:12, email:"jill@someweb.com"}
    ];

    var vSorted:Vector. = Vector.((v as Array).sortOn(“id”, Array.NUMERIC));

Leave a Reply

Spam protection by WP Captcha-Free