隨筆-4  評論-7  文章-0  trackbacks-0
          Array排序
          protected function applicationCompleteHandler(event:FlexEvent):void
          {
              var array:Array = [];
              array.push(new Vga("a",10));
              array.push(new Vga("c",2));
              array.push(new Vga("f",1.3));
              array.push(new Vga("d",1.1));
              array.push(new Vga("e",16));
              array.push(new Vga("b",0));
              trace(array.toString());
              //output:   [a,10],[c,2],[f,1.3],[d,1.1],[e,16],[b,0]
              var defaultSort:Array = array.sort();//默認排序
              trace(defaultSort.toString());
              //output:   [a,10],[b,0],[c,2],[d,1.1],[e,16],[f,1.3]
              var sortFunArray:Array = array.sort(sortFun);//使用自定義方法排序
              trace(sortFunArray.toString());
              //output:   [b,0],[d,1.1],[f,1.3],[c,2],[a,10],[e,16]
          }

          /**自定義排序方法*/            
          public function sortFun(a:Vga,b:Vga):int{
              if(a.price < b.price){
              return -1; //a在前,b在后
              }else if(a.price == b.price){
              return 0; //ab位置不變
              }else{
              return 1; //b在前,a在后
              }
          }



          /**排序VO對象*/
          public class Vga
          {
              public var name:String;
              public var price:Number;
              
              public function Vga(name:String,price:Number)
              {
              this.name = name;
              this.price = price;
              }
                  
              public function toString():String{
              return "["+this.name+","+this.price+"]";
              }
          }


          ArrayCollection排序
          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
              <mx:Script>
                  <![CDATA[
                      import mx.collections.SortField;
                      import mx.collections.Sort;
                      import mx.collections.ArrayCollection;
                      private var acSort:ArrayCollection=
                      new ArrayCollection([{id:0,userName:"zhangSan",age:21},
                                          {id:2,userName:"liSi",age:24},
                                          {id:1,userName:"wangWu",age:31}]);
                      
                      
                      private function sortAc():ArrayCollection{
                          var sort:Sort=new Sort();
                          //按照ID升序排序
                          sort.fields=[new SortField("id")];
                          
                          //按照userName降序排序
                          sort.fields=[new SortField("userName",true,true)];
                          
                          //先按ID升序,再按userName降序
                          sort.fields[new SortField("id"),new SortField("userName",true,true)];
                          acSort.sort=sort;
                          acSort.refresh();//更新
                          return acSort;
                      }
                      
                      /*
                      其實看看API就一目了然
                      SortField () 構造函數(shù) 

                      public function SortField(name:String = null, 
                                  caseInsensitive:Boolean = false, 
                                  descending:Boolean = false, 
                                  numeric:Object = null)
                                              
                      參數(shù)  
                      name:String (default = null) — 此字段用來進行比較的屬性的名稱。如果該對象為簡單類型,則傳遞 null。 
                      caseInsensitive:Boolean (default = false) — 在對字符串進行排序時,指示比較運算符是否忽略值的大小寫。
                      descending:Boolean (default = false) — 指示比較運算符是否按降序排列項目。  
                           
                      numeric:Object (default = null) — 指示比較運算符是否按編號而不按字母順序比較排序項目。 
                      
          */            
                  ]]>
              </mx:Script>
          </mx:Application>
          posted on 2013-03-17 12:19 wkkyo 閱讀(4428) 評論(0)  編輯  收藏 所屬分類: Flex

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 南投市| 黔东| 西乡县| 凉城县| 获嘉县| 郎溪县| 乐至县| 新乐市| 多伦县| 南乐县| 莒南县| 加查县| 普定县| 比如县| 贵港市| 南召县| 张家港市| 霍林郭勒市| 东明县| 丰县| 科技| 惠来县| 沽源县| 六枝特区| 遂昌县| 文化| 莱芜市| 瓮安县| 屯留县| 贵定县| 阳城县| 义乌市| 中山市| 泽普县| 广灵县| 伊金霍洛旗| 会宁县| 丹东市| 桐庐县| 锡林浩特市| 普陀区|