2012-02-24 7 views
6

ArrayCollectionをfieldNameで昇順にソートするとします。ここに私のコードがあり、それが正しいかどうかを知りたい。何か提案はありますか?FlexでArrayCollectionを並べ替える方法

public static function arrayCollectionSort(ar:ArrayCollection, fieldName:String, isNumeric:Boolean):void 
    {var dataSortField:SortField = new SortField(); 
     dataSortField.name = fieldName; 
     dataSortField.numeric = isNumeric; 
     var numericDataSort:Sort = new Sort(); 
     numericDataSort.fields = [dataSortField]; 
     arrCol.sort = numericDataSort; 
     arrCol.refresh();} 

答えて

16

でソートを使用する方法の完全な例です。 arrColarである必要があります。コードは正確に正しいブログFlex Examplesのコードとほぼ同じように見えます。数値が、そうでない場合は、他のすべてが正しいと

public static function arrayCollectionSort(ar:ArrayCollection, fieldName:String, isNumeric:Boolean):void 
{ 
    var dataSortField:SortField = new SortField(); 
    dataSortField.name = fieldName; 
    dataSortField.numeric = isNumeric; 
    var numericDataSort:Sort = new Sort(); 
    numericDataSort.fields = [dataSortField]; 
    ar.sort = numericDataSort; 
    ar.refresh(); 
} 

わからない:

だけの変更は、以下のようにararrColを変更です。

3

コードは問題ありません。ここでは、ボタンクリックで数字とアルファベットの並べ替えが適用される例がいくつかあります。

アルファベット順は、2つの属性のソートの良い例です。この場合、プライマリソートは 'firstname'で行われ、セカンダリソートは 'lastname'で行われます。

ソートフィールドの数値パラメータにブール値trueを指定した場合、ソートは属性を数値にキャストし、数値でソートします。ブール値falseを指定すると、組み込みの文字列比較関数が使用されます。各データ項目は、比較の前にString()関数にキャストされます。デフォルト値のnullの場合、最初のデータ項目はイントロスペクトされ、数字か文字列かどうかが調べられ、そのイントロスペクションに基づいてソートが行われます。また、ここで

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" minWidth="955" minHeight="600"> 

    <mx:Button label="Sort by first then last name" click="sortItemsByName()"/> 
    <mx:Button label="Sort by number" click="sortItemsByNumber()"/> 

    <mx:DataGrid dataProvider="{items}" 
       width="300" 
       height="300"> 
     <mx:columns> 
      <mx:DataGridColumn dataField="number"/> 
      <mx:DataGridColumn dataField="firstname"/> 
      <mx:DataGridColumn dataField="lastname"/> 
     </mx:columns> 
    </mx:DataGrid> 

    <mx:ArrayCollection id="items"> 
     <mx:Object number="3" firstname="John" lastname="Brown"/> 
     <mx:Object number="1" firstname="Kate" lastname="Brown"/> 
     <mx:Object number="4" firstname="Jeremy" lastname="Ryan"/> 
     <mx:Object number="5" firstname="Joe" lastname="Wilson"/> 
     <mx:Object number="2" firstname="Greg" lastname="Walling"/> 
    </mx:ArrayCollection> 

    <mx:Script> 
     <![CDATA[   
      import mx.collections.ArrayCollection; 
      import mx.collections.Sort; 
      import mx.collections.SortField; 

      /** 
      * Sort the arraycollection by the firstname and then the last name 
      * */ 
      private function sortItemsByName():void{ 
       var srt:Sort = new Sort(); 
       srt.fields = [new SortField("firstname"), new SortField("lastname")]; 
       items.sort = srt; 
       items.refresh(); 
      } 

      /** 
      * Sort the arraycollection numerically 
      * */ 
      private function sortItemsByNumber():void{ 
       var srt:Sort = new Sort(); 
       srt.fields = [new SortField("number", true, false, true)]; 
       items.sort = srt; 
       items.refresh(); 
      } 

     ]]> 
    </mx:Script> 
</mx:Application> 

は... SORTFIELDの言語リファレンスです

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/collections/SortField.html

...とデータプロバイダとコレクションのためのAdobe LiveDocsの参照...

http://livedocs.adobe.com/flex/3/html/help.html?content=about_dataproviders_2.html

...ここではソートとフィルタリングのための良いlivedocsリファレンスがあります。

http://livedocs.adobe.com/flex/3/html/help.html?content=about_dataproviders_4.html

関連する問題