私は解決策を見つけました。私dataField hour
は挿入時間なので、アイテムを追加すると、必ずこのアイテムが最後に配置されます。私のテストコードはこれです:
<s:Button label="add" click="generateData(event)" />
<mx:AdvancedDataGrid width="500" height="800">
<mx:dataProvider>
<mx:GroupingCollection2 id="gc" source="{ac}">
<mx:grouping>
<mx:Grouping compareFunction="compare">
<mx:GroupingField name="a" />
<mx:GroupingField name="b" />
</mx:Grouping>
</mx:grouping>
</mx:GroupingCollection2>
</mx:dataProvider>
<mx:columns>
<mx:AdvancedDataGridColumn dataField="a" />
<mx:AdvancedDataGridColumn dataField="b" />
<mx:AdvancedDataGridColumn dataField="hour" />
</mx:columns>
</mx:AdvancedDataGrid>
[Bindable]
private var ac:ArrayCollection = new ArrayCollection([]);
// Array of "a" property order
private var order:Array = [];
protected function generateData(event:MouseEvent):void
{
// test data
var t:Array = [
{a:"a_1", b:"b_1", hour:null},
{a:"a_1", b:"b_1", hour:null},
{a:"a_1", b:"b_1", hour:null},
{a:"a_1", b:"b_2", hour:null},
{a:"a_1", b:"b_2", hour:null},
{a:"a_2", b:"b_2", hour:null},
{a:"a_2", b:"b_2", hour:null},
{a:"a_2", b:"b_2", hour:null},
{a:"a_2", b:"b_3", hour:null},
{a:"a_3", b:"b_3", hour:null},
{a:"a_3", b:"b_4", hour:null},
{a:"a_3", b:"b_4", hour:null},
{a:"a_3", b:"b_4", hour:null},
{a:"a_4", b:"b_5", hour:null},
{a:"a_5", b:"b_6", hour:null},
{a:"a_5", b:"b_6", hour:null},
];
// random number of insertions
var toAddNumber:int = 10 * int(1 + Math.random());
var index:int;
// "a" type added with random insertions
var aAdded:Array = [];
var o:Object;
for(var i:int=0; i<toAddNumber; i++)
{
// random item of t added
index = (Math.floor(Math.random() * (t.length-1 - 0 + 1)) + 0);
o = t[index];
// hour initialized
o.hour = getTimer();
// we insert this item in dataProvider
ac.addItem(o);
if(aAdded.indexOf(o["a"]) == -1)
{
aAdded.push(o["a"]);
}
}
// Natural sort
aAdded.sort();
// update of order tab
for(var s:String in aAdded)
{
if(order.indexOf(aAdded[s]) != -1)
{
order.splice(order.indexOf(aAdded[s]), 1);
}
order.push(aAdded[s]);
}
// we update GroupingCollection2
gc.refresh();
}
private function compare(o1:Object, o2:Object, fields:Array = null):int
{
// order in order tab...
if(order.indexOf(o1.a) > order.indexOf(o2.a))
{
return 1;
}
else if(order.indexOf(o1.a) < order.indexOf(o2.a))
{
return -1;
}else
{
// ... then natural order on "b"...
if(o1.b > o2.b)
{
return 1;
}
else if(o1.b < o2.b)
{
return -1;
}
else{
// ... then numeric order on hour field
if(o1.hour > o2.hour)
{
return 1;
}
else if(o1.hour < o2.hour)
{
return -1;
}
else{
return 0;
}
}
}
}
そして、それはうまくいきます:)