在下面的代码中 GroupCollection2 比较函数将一组分成 2 组。当我删除比较功能时,它工作正常,但是当将比较添加到排序列时,它会将一组分成不同的组。
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" >
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<fx:XMLList id="defaultColumns">
<ROW>
<NAME>A</NAME>
<COLUMN_SRT_ORDR>4</COLUMN_SRT_ORDR>
<GROUP_DESCRIPTION>1. Description</GROUP_DESCRIPTION>
</ROW>
<ROW>
<NAME>B</NAME>
<COLUMN_SRT_ORDR>11</COLUMN_SRT_ORDR>
<GROUP_DESCRIPTION>1. Description</GROUP_DESCRIPTION>
</ROW>
<ROW>
<NAME>C</NAME>
<COLUMN_SRT_ORDR>36</COLUMN_SRT_ORDR>
<GROUP_DESCRIPTION>1. Description</GROUP_DESCRIPTION>
</ROW>
<ROW>
<NAME>D</NAME>
<COLUMN_SRT_ORDR>41</COLUMN_SRT_ORDR>
<GROUP_DESCRIPTION>1. Description</GROUP_DESCRIPTION>
<OPTIONS/>
</ROW>
<ROW>
<NAME>E</NAME>
<COLUMN_SRT_ORDR>2</COLUMN_SRT_ORDR>
<GROUP_DESCRIPTION>2.Budget</GROUP_DESCRIPTION>
</ROW>
<ROW>
<NAME>F</NAME>
<COLUMN_SRT_ORDR>6</COLUMN_SRT_ORDR>
<GROUP_DESCRIPTION>2.Budget</GROUP_DESCRIPTION>
</ROW>
<ROW>
<NAME>G</NAME>
<COLUMN_SRT_ORDR>7</COLUMN_SRT_ORDR>
<GROUP_DESCRIPTION>2.Budget</GROUP_DESCRIPTION>
</ROW>
<ROW>
<NAME>G</NAME>
<GROUP_DESCRIPTION>DEBUG</GROUP_DESCRIPTION>
<COLUMN_SRT_ORDR>10003</COLUMN_SRT_ORDR>
<OPTIONS/>
</ROW>
<ROW>
<NAME>H</NAME>
<GROUP_DESCRIPTION>DEBUG</GROUP_DESCRIPTION>
<COLUMN_SRT_ORDR>10005</COLUMN_SRT_ORDR>
<OPTIONS/>
</ROW>
<ROW>
<NAME>I</NAME>
<COLUMN_CAPTION>Sql</COLUMN_CAPTION>
<COLUMN_WIDTH>1</COLUMN_WIDTH>
<GROUP_DESCRIPTION>DEBUG</GROUP_DESCRIPTION>
<COLUMN_SRT_ORDR>10006</COLUMN_SRT_ORDR>
<OPTIONS/>
</ROW>
</fx:XMLList>
<mx:GroupingCollection2 id="colGroup" source="{defaultColumns}">
<mx:grouping>
<mx:Grouping>
<mx:GroupingField name="GROUP_DESCRIPTION" compareFunction="myCompare"/>
</mx:Grouping>
</mx:grouping>
</mx:GroupingCollection2>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.events.IndexChangedEvent;
private function myCompare(a:Object, b:Object):int{
var result:int=-1;
if(parseInt(a.COLUMN_SRT_ORDR)<parseInt(b.COLUMN_SRT_ORDR))
result=-1;
else if(parseInt(a.COLUMN_SRT_ORDR)==parseInt(b.COLUMN_SRT_ORDR))
result=0;
else
result=1;
return result;
}
]]>
</fx:Script>
<mx:TabNavigator id="groupTabs" borderStyle="solid" width="100%" initialize="{colGroup.refresh()}" >
<mx:Repeater dataProvider="{colGroup.getRoot()}" id="t">
<mx:VBox label="{t.currentItem.GroupLabel}" width="100%" height="100%">
</mx:VBox>
</mx:Repeater>
</mx:TabNavigator>
</s:Application>