我正在尝试根据另一个 XMLListCollection 的属性对 XMLListCollection 进行排序,但没有取得多大成功。
我想将一组食物按食用顺序排序 - 存储在另一个 XMLListcollection 中。
<?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>
<fx:XML xmlns="" id="_foods">
<data>
<node label="Apple" id="A"/>
<node label="Banana" id="B"/>
<node label="Carrot" id="C"/>
<node label="Dandelion" id="D"/>
</data>
</fx:XML>
<fx:XML xmlns="" id="_orders">
<data>
<order id="C"/>
<order id="A"/>
<order id="D"/>
<order id="B"/>
</data>
</fx:XML>
<s:XMLListCollection id="_orderList" source=" {_orders.children()}"/>
<s:XMLListCollection id="_foodCollection" source="{_foods.children()}" sort="{_foodSort}"/>
<s:Sort id="_foodSort" compareFunction="sortFruits"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
protected function sortFruits(a:Object, b:Object, fields:Array = null):int
{
var _currentItem:XML = XML(a);
var _currentOrderIndex:int= getNodeIndexByAttribute(_orderList,'id',_currentItem.@id);
var _currentFruitIndex:int= getNodeIndexByAttribute(_foodCollection,'id',_currentItem.@id)
if(_currentFruitIndex > _currentOrderIndex) {_returnData =1;}
else
if(_currentFruitIndex < _currentOrderIndex) { _returnData =-1;}
else {_returnData = 0}
var _returnData:int = 0
return _returnData
}
public function getNodeIndexByAttribute(theCollection:XMLListCollection,theAttributeName:String, theAttributeValue:String):int {
//THIS IS RETURNS THE INDEX OF A CHILD NODE BASED UPON AN ATTRIBUTE THAT IS PASSED IN - GENERIC FUNCTION
var _returnData:int
for (var i:int = 0; i < theCollection.length; i++)
{
var _currentItem:XML = theCollection.getItemAt(i) as XML
if (_currentItem.attribute(theAttributeName) == theAttributeValue) { _returnData = i;
break;
}
}
return _returnData;
}
]]>
</fx:Script>
<s:List width="100%" height="100%" labelField="@label" dataProvider="{_foodCollection}"/>
</s:Application>
据我所知,我似乎做的一切都是正确的,但我一定做错了什么,因为它不起作用!我认为这可能与我实现比较功能的方式有关。
对此的任何帮助将不胜感激!;)