{"id":249,"date":"2009-04-11T09:10:56","date_gmt":"2009-04-11T16:10:56","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1149"},"modified":"2009-04-11T09:10:56","modified_gmt":"2009-04-11T16:10:56","slug":"vectorsorton","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=249","title":{"rendered":"Vector.sortOn()"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar a:Vector.&lt;Sprite&gt; = new Vector.&lt;Sprite&gt;();\r\n\r\ntrace(&quot;unsorted&quot;);\r\nfor (var i:int = 0; i&lt;10; i++){\r\n\tvar s:Sprite = new Sprite();\r\n\ts.x = int(Math.random()*100);\r\n\ta.push(s);\r\n\ttrace(s.x);\r\n}\r\n\r\nquickSortOn(a, &quot;x&quot;, 0, a.length-1);\r\n\r\ntrace(&quot;sorted&quot;);\r\nfor (i= 0; i&lt;10; i++){\r\n\ttrace(a&#x5B;i].x);\r\n}\r\n\r\n\/\/ modified code from kirupa.com\r\n\/\/ http:\/\/www.kirupa.com\/developer\/actionscript\/quickSort.htm\r\nfunction quickSortOn(a:Vector.&lt;Sprite&gt;, prop:String, left:int, right:int):void {\r\n    var i:int = 0, j:int = 0, pivot:Sprite, tmp:Sprite;\r\n\ti=left;\r\n\tj=right;\r\n\tpivot = a&#x5B;Math.round((left+right)*.5)];\r\n\twhile (i&lt;=j) {\r\n\t\twhile (a&#x5B;i]&#x5B;prop]&lt;pivot&#x5B;prop]) i++;\r\n\t\twhile (a&#x5B;j]&#x5B;prop]&gt;pivot&#x5B;prop]) j--;\r\n\t\tif (i&lt;=j) {\r\n\t\t\ttmp=a&#x5B;i];\r\n\t\t\ta&#x5B;i]=a&#x5B;j];\r\n\t\t\ti++;\r\n\t\t\ta&#x5B;j]=tmp;\r\n\t\t\tj--;\r\n\t\t}\r\n\t}\r\n\tif (left&lt;j)  quickSortOn(a, prop, left, j);\r\n\tif (i&lt;right) quickSortOn(a, prop, i, right);\r\n}\r\n\/* outputs something like:\r\nunsorted\r\n26\r\n33\r\n20\r\n63\r\n7\r\n68\r\n75\r\n39\r\n67\r\n53\r\nsorted\r\n7\r\n20\r\n26\r\n33\r\n39\r\n53\r\n63\r\n67\r\n68\r\n75\r\n*\/\r\n<\/pre>\n<p>This demo is my first quick stab at using at a sortOn() function for the Vector class. It sorts a Vector of Sprites by their x property. <\/p>\n<p>Recently there were a few times when I was prototyping ideas and suddenly realized that I needed to change my Vector to an Array because I needed to use sortOn().(If you don&#8217;t already know, there is no built in sortOn() method for the Vector class). In the past I spent some time with sorting algorithms, bubble, insertion etc&#8230; so I knew I could pretty easily write my own sortOn(), but I also realized that a generic implementation wouldn&#8217;t be easy\/possible without loosing the type of the Vector. What I mean is, if you have a Vector of Sprites, you need a sorting method that takes a Vector.< Sprite > type as an argument (as seen above), if you have a Vector of TextFields you need a Vector.< TextField > type as an argument. You could of course use a generic type, but this kind of defeats the purpose of using a vector in the first place&#8230;<\/p>\n<p>I will likely post a revised version of this in the near future with a slightly improved implementation of QuickSort. I haven&#8217;t spent that much time with this, but If I recall correctly this is not the ideal implementation. I ported this code from <a href=\"http:\/\/www.kirupa.com\/developer\/actionscript\/quickSort.htm\">a nice Kirupa tutorial<\/a> and modified it to sort based on a property&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>var a:Vector.&lt;Sprite&gt; = new Vector.&lt;Sprite&gt;(); trace(&quot;unsorted&quot;); for (var i:int = 0; i&lt;10; i++){ var s:Sprite = new Sprite(); s.x = int(Math.random()*100); a.push(s); trace(s.x); } quickSortOn(a, &quot;x&quot;, 0, a.length-1); trace(&quot;sorted&quot;); for (i= 0; i&lt;10; i++){ trace(a&#x5B;i].x); } \/\/ modified code from kirupa.com \/\/ http:\/\/www.kirupa.com\/developer\/actionscript\/quickSort.htm function quickSortOn(a:Vector.&lt;Sprite&gt;, prop:String, left:int, right:int):void { var i:int = 0, j:int = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,32,7,47,79],"tags":[3,4],"class_list":["post-249","post","type-post","status-publish","format-standard","hentry","category-arrays","category-associative-arrays","category-object","category-sorton","category-vector","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=249"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/249\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}