使用 sortfield 对多个属性的 arraycollection 进行排序未按预期工作

Sorting an arraycollection on multiple properties using sortfield is not working as expected

下面的排序比较函数按预期工作,但如果我将 descending 设置为 true,则不会。

var dataSortField:SortField = new SortField("y", false, false, true);
var dataSortField2:SortField = new SortField("x", false, true, true);

dataSortField2.compareFunction = 
    function sort(item1:Object, item2:Object):int {
        return -1 * (ObjectUtil.numericCompare(item1.x, item2.x));
    }

谁能告诉我为什么?

您不需要为 Ascending/Descending[=20= 使用 truefalse ] 因为你有一个自定义 compareFunction。这是一个基本示例,您可以如何使用它。

<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script><![CDATA[
    import mx.collections.ArrayCollection;
    import mx.utils.ObjectUtil;
    import spark.collections.Sort;
    import spark.collections.SortField;

    private var arrayCollection:ArrayCollection = new ArrayCollection([
        {x:5, Name:"Adam Lee"},
        {x:2, Name:"Bryan Hobb"},
        {x:3, Name:"Claudia Toledo"},
        {x:7, Name:"Matt Brill"},
        {x:8, Name:"Justin Hinz"},
        {x:4, Name:"Bikram Dangol"}
    ])

    private function onAscendingButtonClick():void
    {
        var dataSortField2:SortField = new SortField();
        dataSortField2.compareFunction = sortAscendingCompareFunction;
        arrayCollection.sort = new Sort();
        arrayCollection.sort.fields = [dataSortField2];
        arrayCollection.refresh();
    }

    private function sortAscendingCompareFunction(item1:Object, item2:Object):int
    {
        return ObjectUtil.numericCompare(item1.x, item2.x);
    }

    private function sortDesscendingCompareFunction(item1:Object, item2:Object):int
    {
        return ObjectUtil.numericCompare(item2.x, item1.x);
    }

    private function onDescendingButtonClick():void
    {
        var dataSortField2:SortField = new SortField();
        dataSortField2.compareFunction = sortDesscendingCompareFunction;
        arrayCollection.sort = new Sort();
        arrayCollection.sort.fields = [dataSortField2];
        arrayCollection.refresh();
    }
    ]]>
</fx:Script>
<s:VGroup verticalCenter="0" horizontalCenter="0">
    <s:HGroup>
        <s:Button label="Sort Ascending" click="{onAscendingButtonClick()}"/>
        <s:Button label="Sort Descending" click="{onDescendingButtonClick()}"/>
    </s:HGroup>
    <s:Panel title="Sort using compareFunction" width="100%" backgroundColor="green">
        <s:DataGrid dataProvider="{arrayCollection}" sortableColumns="false" width="100%"/>
    </s:Panel>
</s:VGroup>
</s:Application>

如果您不想使用自定义 compareFunction 那么这就是实现 ascending/descending 功能的方法:

<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script><![CDATA[
    import mx.collections.ArrayCollection;
    import mx.utils.ObjectUtil;
    import spark.collections.Sort;
    import spark.collections.SortField;

    private var arrayCollection:ArrayCollection = new ArrayCollection([
        {x:5, Name:"Adam Lee"},
        {x:2, Name:"Bryan Hobb"},
        {x:3, Name:"Claudia Toledo"},
        {x:7, Name:"Matt Brill"},
        {x:8, Name:"Justin Hinz"},
        {x:4, Name:"Bikram Dangol"}
    ])

    private function onAscendingButtonClick():void
    {
        var dataSortField2:SortField = new SortField("x", false, true);
        //dataSortField2.compareFunction = sortAscendingCompareFunction;
        arrayCollection.sort = new Sort();
        arrayCollection.sort.fields = [dataSortField2];
        arrayCollection.refresh();
    }

    private function sortAscendingCompareFunction(item1:Object, item2:Object):int
    {
        return ObjectUtil.numericCompare(item1.x, item2.x);
    }

    private function sortDesscendingCompareFunction(item1:Object, item2:Object):int
    {
        return ObjectUtil.numericCompare(item2.x, item1.x);
    }

    private function onDescendingButtonClick():void
    {
        var dataSortField2:SortField = new SortField("x", true, true);
        //dataSortField2.compareFunction = sortDesscendingCompareFunction;
        arrayCollection.sort = new Sort();
        arrayCollection.sort.fields = [dataSortField2];
        arrayCollection.refresh();
    }
    ]]>
</fx:Script>
<s:VGroup verticalCenter="0" horizontalCenter="0">
    <s:HGroup>
        <s:Button label="Sort Ascending" click="{onAscendingButtonClick()}"/>
        <s:Button label="Sort Descending" click="{onDescendingButtonClick()}"/>
    </s:HGroup>
    <s:Panel title="Sort without compareFunction" width="100%" backgroundColor="green">
        <s:DataGrid dataProvider="{arrayCollection}" sortableColumns="false" width="100%"/>
    </s:Panel>
</s:VGroup>
</s:Application>

看起来你想使用多个 SortField。首先按 Field ascending 顺序排序。如果它们相同,则按 降序 顺序按第二个 Field 排序。如果您使用 compareFunction,则优先。在你的情况下,不要使用 compareFunction 因为你使用 descending 作为 true for SortField x . 下面是一个例子:

<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
           xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script><![CDATA[
    import mx.collections.ArrayCollection;
    import spark.collections.Sort;
    import spark.collections.SortField;

    private var arrayCollection:ArrayCollection = new ArrayCollection([
        {x:3, Name:"Claudia Toledo"},
        {x:5, Name:"Adam Lee"},
        {x:7, Name:"Adam Lee"},
        {x:2, Name:"Bryan Hobb"},
        {x:7, Name:"Matt Brill"},
        {x:4, Name:"Bikram Dangol"},
        {x:4, Name:"Adam Lee"},
        {x:9, Name:"Bikram Dangol"},
        {x:8, Name:"Justin Hinz"},
        {x:2, Name:"Bikram Dangol"}
    ]);

    private function onSortButtonClick():void
    {
        var dataSortField1:SortField = new SortField("Name", false, false);
        var dataSortField2:SortField = new SortField("x", true, true);
        arrayCollection.sort = new Sort();
        arrayCollection.sort.fields = [dataSortField1,dataSortField2];
        arrayCollection.refresh();
    }


    ]]>
</fx:Script>
<s:VGroup verticalCenter="0" horizontalCenter="0">
        <s:Button label="Sort Ascending Name and Descending x" click="{onSortButtonClick()}"/>
    <s:Panel title="Multiple Field Sort" width="100%" backgroundColor="green">
        <s:DataGrid dataProvider="{arrayCollection}" sortableColumns="false" width="100%"/>
    </s:Panel>
</s:VGroup>
</s:Application>