6

我想按 fieldName 对 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();}
4

3 回答 3

16

您拥有的代码是正确的,但类型除外。arrCol应该是ar。该代码看起来几乎与博客Flex 示例中的代码一模一样,这也是正确的。

只需更改为arrCol如下ar所示:

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();
}

不确定数字,但其他一切都是正确的。

于 2012-02-24T11:20:23.957 回答
3

这是如何在数组集合中使用排序的完整示例

http://blog.flexexamples.com/2007/08/05/sorting-an-arraycollection-using-the-sortfield-and-sort-classes/

于 2012-02-24T11:16:04.140 回答
3

您的代码很好,即使这里有几个示例,在按钮单击时应用了数字和字母排序。

字母排序是对 2 个属性进行排序的一个很好的例子。在这种情况下,主要排序是在“名字”上完成的,次要排序是在“姓氏”上完成的。

数字排序非常灵活,如果你为排序字段的数字参数提供一个布尔值 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

...以及数据提供者和集合的 Adob​​e 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

于 2012-02-29T22:54:35.637 回答