2011年6月10日 星期五

亂數取值存入陣列_Array.sort()方法排序(Array.NUMERIC常數、Array.DESCENDING常數)

sort(... args):Array
對陣列中的元素進行排序
此例, 亂數產生100個數字, 存入陣列, 採用Array.sort()方法排序.
Array.NUMERIC常數 : 將陣列元素視為數值,且預設為升冪排序.
Array.DESCENDING常數 : 降冪排序.

1000608_Array.sort.swf


package {

 import flash.display.MovieClip;
 import flash.text.TextFormat;
 import flash.text.TextField;
 import fl.controls.UIScrollBar;
 import fl.controls.ScrollBarDirection;
 import flash.events.MouseEvent;
 import flash.events.Event;
 import flash.ui.Mouse;

 public class ArraySort_1000608 extends MovieClip {

  private var figureArr:Array;
  private var vScrollBar:UIScrollBar;

  public function ArraySort_1000608() {
   this.init();
   this.addMouseEvent();
   return;
  }

  private function init():void {
   this.vScrollBar = new UIScrollBar();
   this.createUIScrollBar();
   return;
  }

  private function addMouseEvent():void {
   this.newRandomArrayBtn.buttonMode = true;
   this.newRandomArrayBtn.addEventListener(MouseEvent.CLICK,this.iniRandomArray);
   this.sortArrayNumericBtn.buttonMode = true;
   this.sortArrayNumericBtn.addEventListener(MouseEvent.CLICK,this.sortArrayNumeric);
   this.sortArrayDescendingBtn.buttonMode = true;
   this.sortArrayDescendingBtn.addEventListener(MouseEvent.CLICK,this.sortArrayDescending);
   return;
  }

  /*亂數取值100次*/
  private function iniRandomArray(event:MouseEvent):void {
   this.figureArr = new Array();
   for (var i:uint=0; i<100; i++) {
    this.getRandomValue();
   }
   this.showInfo();
   return;
  }

  /*亂數取值, 存入陣列*/
  private function getRandomValue():void {
   var newFigure:uint = Math.ceil(Math.random() * 1000);
   this.figureArr.push(newFigure);
   return;
  }

  private function sortArrayNumeric(event:MouseEvent):void {
   if(this.figureArr != null){
    /*將元素視為數值, 且升冪排序 - Array.NUMERIC ; 將元素視為數值,且預設升冪排序*/
    this.figureArr.sort(Array.NUMERIC);
    this.showInfo();
   }
   return;
  }

  private function sortArrayDescending(event:MouseEvent):void {
   if(this.figureArr != null){
    /*將元素視為數值, 且降冪排序 - Array.DESCENDING : 降冪排序*/
    this.figureArr.sort(Array.NUMERIC | Array.DESCENDING);
    this.showInfo();
   }
   return;
  }

  private function showInfo():void {
   trace(figureArr + "\n**************************************************");
   addInfo(figureArr + "\n**************************************************");
   /*強制捲軸立即更新其捲動屬性。 - 重要,否則捲軸拖曳Bar會不見*/
   this.vScrollBar.scrollPosition = this.vScrollBar.maxScrollPosition;
   trace("this.vScrollBar.scrollPosition : " + this.vScrollBar.scrollPosition);
   this.vScrollBar.update();
   return;
  }

  /*設置info動態文字欄位屬性*/
  private function setInfoTF():void {
   var format:TextFormat = new TextFormat();
   format.color = 0x000000;
   this.info.selectable = true;
   this.info.wordWrap = true;
   /*autoSize屬性不能開, 否則捲軸會不正常*/
   /*this.info.autoSize = TextFieldAutoSize.LEFT;*/
   this.info.setTextFormat(format);
   this.info.defaultTextFormat = format;
   return;
  }

  /*附加字串至TextField 欄位目前字串之後*/
  private function addInfo(_obj:*):void {
   this.info.appendText(_obj.toString() + "\n");
   return;
  }

  /*初始定義UIScrollBar屬性*/
  private function createUIScrollBar():void {
   this.vScrollBar = new UIScrollBar();
   this.vScrollBar.direction = ScrollBarDirection.VERTICAL;
   this.vScrollBar.scrollTarget = info;
   this.vScrollBar.move(info.x + info.width + 10, info.y);
   this.vScrollBar.height = info.height;
   this.addChild(vScrollBar);
   return;
  }
 }

}



適用於 Adobe® Flash® Platform 的 ActionScript® 3.0 參考
最上層 - Array - sort()方法

沒有留言: