以下使用排序比较功能正常工作,但如果我将降序设置为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));
}
谁能告诉我为什么?
答案 0 :(得分:0)
您不需要使用 true 或 false 来升序/降序,因为您有自定义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>
答案 1 :(得分:0)
如果您不想使用自定义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("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>
答案 2 :(得分:0)
您希望使用多个SortField
。首先按升序顺序按第一个字段排序。如果它们相同,则按降序顺序按第二个字段排序。如果您使用compareFunction
,则优先。在您的情况下,请不要使用compareFunction
,因为true
x 使用降序作为SortField
。
以下是一个例子:
<?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>