Advertisement
angelhdz12

Array Sort and Search

Mar 23rd, 2017
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import flash.events.MouseEvent;
  2. import fl.controls.TextArea;
  3.  
  4. //Declare numbersArray Array and define it as an empty array
  5. var numbersArray:Array = [];
  6.  
  7. //Restrict the input textfields to only numbers
  8. registerTextField.restrict = "0-9";
  9. searchTextField.restrict = "0-9";
  10.  
  11. //Register
  12. registerButton.addEventListener( MouseEvent.CLICK, onRegisterClick );
  13. function onRegisterClick( event:MouseEvent ):void
  14. {
  15.     //If registerTextField is not empty
  16.     if( registerTextField.length )
  17.     {
  18.         //Push the typed number in the numbersArray
  19.         numbersArray.push( Number( registerTextField.text ) );
  20.         //Update the outputTextArea
  21.         update(registerTextField.text+" registered.");
  22.         //Clear the registerTextField
  23.         registerTextField.text = "";
  24.         //Give registerTextField the focus
  25.         stage.focus = registerTextField;
  26.     }
  27. };
  28.  
  29. //Function which returns a string containing all the numbers
  30. //separated by line breaks
  31. function parseNumbers():String
  32. {
  33.     var sorted:String = "";
  34.     for(var k:String in numbersArray) sorted += String(numbersArray[k]) + "\n";
  35.     return sorted;
  36. };
  37.  
  38. //Ascending
  39. ascendingButton.addEventListener( MouseEvent.CLICK, onAscendingClick );
  40. function onAscendingClick( event:MouseEvent ):void
  41. {
  42.     //If numbersArray is not empty
  43.     if(numbersArray.length)
  44.     {
  45.         for(var i:int = 0; i < numbersArray.length; i++)
  46.         {
  47.             for(var j:int = 0; j < numbersArray.length; j++)
  48.             {
  49.                 if(numbersArray[j] > numbersArray[j+1])
  50.                 {
  51.                     var temp:Number = numbersArray[j];
  52.                     numbersArray[j] = numbersArray[j+1];
  53.                     numbersArray[j+1] = temp;
  54.                     temp = NaN;
  55.                 }
  56.             }
  57.         }
  58.         //Update the TextArea
  59.         update("Sorting in ascending order...\n"+parseNumbers());
  60.     }
  61. };
  62.  
  63. //Descending
  64. descendingButton.addEventListener( MouseEvent.CLICK, onDescendingClick );
  65. function onDescendingClick( event:MouseEvent ):void
  66. {
  67.     //If numbersArray is not empty
  68.     if(numbersArray.length)
  69.     {
  70.         for(var i:int = 0; i < numbersArray.length; i++)
  71.         {
  72.             for(var j:int = 0; j < numbersArray.length; j++)
  73.             {
  74.                 if(numbersArray[j] < numbersArray[j+1])
  75.                 {
  76.                     var temp:Number = numbersArray[j];
  77.                     numbersArray[j] = numbersArray[j+1];
  78.                     numbersArray[j+1] = temp;
  79.                     temp = NaN;
  80.                 }
  81.             }
  82.         }
  83.         //Update the TextArea
  84.         update("Sorting in descending order...\n"+parseNumbers());
  85.     }
  86. };
  87.  
  88. //Search
  89. searchButton.addEventListener( MouseEvent.CLICK, onSearchClick );
  90. function onSearchClick( event:MouseEvent ):void
  91. {
  92.     //If searchTextField is not empty
  93.     if(searchTextField.length)
  94.     {
  95.         //Search the typed number in numbersArray and return the index
  96.         var index:int = numbersArray.indexOf( Number( searchTextField.text ) );
  97.        
  98.         //If the number is found and the array is not empty
  99.         if( index > -1 && numbersArray.length )
  100.         {
  101.             //Update the TextArea
  102.             update("Results: " + numbersArray[ index ] + " found at index " + index);
  103.         }
  104.         //else, either the number was not found or the array is empty
  105.         else
  106.         {
  107.             //Update the TextArea
  108.             update("No results.");
  109.         }
  110.         //Clear the searchTextField
  111.         searchTextField.text = "";
  112.         //Give searchTextField focus
  113.         stage.focus = searchTextField;
  114.     }
  115. };
  116.  
  117. //Update function
  118. function update(string:String = null):void
  119. {
  120.     //If string is not empty
  121.     if(string)
  122.     {
  123.         //Append the string in the TextArea
  124.         outputTextArea.appendText( String( string ) + "\n" );
  125.         //Update the TextArea's scrollbar position
  126.         outputTextArea.verticalScrollPosition = outputTextArea.maxVerticalScrollPosition;
  127.     }
  128. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement