actionscript-3apache-flexsortingsplicexmllist

Sort then Splice XMLList or XMLList Collection in AS3


Nothing I try works. I am trying to sort then splice an XMLList or XMLListCollection to some number of results.

Here is my code:

public function chopData(){

//pData = pDataCollection.source;
pData = sortXMLListByAttribute(filteredData, sortField);
var thisLength = pData.length();
trace("thisLength: " + thisLength);
var maxAmountToShow = 30;


if(thisLength == null){
    thisLength = maxAmountToShow;
}

if( thisLength < maxAmountToShow){
    pData = SliceXMLList(pData, 0, thisLength - 1);
    //pDataCollection = SpliceXMLListCollection(pDataCollection, 0, thisLength - 1);
}else{
    pData = SliceXMLList(pData, 0, maxAmountToShow - 1);
    //pDataCollection = SpliceXMLListCollection(pDataCollection, 0, maxAmountToShow - 1);
}


pDataCollection = new XMLListCollection(pData);

}


 public static function sortXMLListByAttribute
 (
    $xmlList        :   XMLList,
    $attribute  :   String,
    $options    :   Object  =   null
) :XMLList
 {
//store in array to sort on
var xmlArray:Array= new Array();
var item:XML;
for each(item in $xmlList)
{
    var object:Object = {
        data    : item, 
        order   : item.attribute($attribute)
    };
    xmlArray.push(object);
}

////sort using the power of Array.sortOn()
xmlArray.sortOn('order',$options);

////create a new XMLList with sorted XML
var sortedXmlList:XMLList = new XMLList();
var xmlObject:Object;
for each(xmlObject in xmlArray )
{
    sortedXmlList += xmlObject.data;
}
return sortedXmlList;   
}

Solution

  • I found this com.maximporges.MaskedCollection class and it works perfectly.

    http://dreamingwell.com/examples/flex/scrollableGraph/srcview/