Skip to content


Sorting Arrays with Array.SortOn Method

This came in handy for my carousel animation. It helped resort the objects z-index based on it’s scale parameter.

package
{
	import flash.display.Sprite;
	import fl.controls.TextArea;
	import fl.controls.ScrollPolicy;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;

	public class ArraySortOn extends Sprite
	{
		private var output:TextArea;
		private var Arr:Array;
		private var ConcatArrays:Array;
		private var msgString:String;

		public function ArraySortOn()
		{
			init()
		}

		private function init():void
		{
			// display our output field
			Output();

			// set up the arrays
			Arr = new Array();
			Arr.push({fname:"ashley", lname:"jackson"});
			Arr.push({fname:"morgan", lname:"newcomb"});
			Arr.push({fname:"andy", lname:"smith"});
			Arr.push({fname:"joe", lname:"blow"});

			// display the output
			msg ( "OUTPUT" );
			msg ( "-----------------------------------" );
			msg ( "ArrOne:\n " + outputArray(Arr) );
			msg ( "Sorting....." );

			Arr.sortOn(["lname", "fname"]);
			msg ( "Last Name then First Name:\n " +	outputArray(Arr));

			Arr.sortOn(["fname", "lname"]);
			msg ( "First Name then Last Name:\n " +  outputArray(Arr));
			msg ( "-----------------------------------" );
		}

		private function outputArray(a:Array)
		{
			msgString = "";
			for ( var i:uint = 0; i < a.length; i++ )
			{
				msgString += a[i].fname + "\t" + a[i].lname + "\n";
			}

			return msgString;
		}

		// UI elements
		public function Output():void
		{
			// Small Black Text Formatting
			var smallBlackFormat:TextFormat = new TextFormat();
			smallBlackFormat.font = "Arial";
			smallBlackFormat.color = 0x000000;
            smallBlackFormat.size = 10;
            smallBlackFormat.underline = false;

        	// output TextField
			output = new TextArea();
           	output.verticalScrollPolicy = ScrollPolicy.ON;
           	output.condenseWhite = true;
           	output.setSize(stage.stageWidth, stage.stageHeight);
           	output.move(0,0);
           	addChild(output);
        }

		public function msg(m:String):void
		{
			output.appendText (m + "\n");
		}

	}
}

Posted in Actionscript 3, Arrays.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.