Hey,
I've been looking at sorting array collections. So far I've seen things like sorting numerically in ascending and descending order. What I'm looking for is to discover away to sort an arraycollection based on the order of values in another array.
For Example:
I have an array containing 10 numerical values. I also have an arraycollection. One of the properties of each arracycollection entry corresponds to one of the values in the former array.
I want to be able to sort the arraycollection based on the order of the values in the array.
Whats the best way to this?
I was thinking of looping through the first array, getting the first value, finding the entry in the arraycollection with that value and then adding it to a new arraycollection but it seems kinda long winded and I was hoping there might be a clever way to do it.
EDIT
This is what I've pieced together so far. Seems a bit too round about though
private function parseXML(xml:XML):void
{
var s:String=xml.toXMLString()
artistinfo=convertXmlToArrayCollection(s)
sort()
dispatchEvent(new XMLEvent(XMLEvent.XML_PARSED))
//artistinfo.sort()
}
private function clone(source:Object):*
{
var myBA:ByteArray=new ByteArray();
myBA.writeObject(source);
myBA.position=0;
return (myBA.readObject());
}
private function sort():void
{
var myAC:ArrayCollection=new ArrayCollection(clone(artistinfo.source));
//artistinfo=new ArrayCollection();
var amt:int=trackids.length;
var value:Number=0
var arr:*
var index:Number
for (var i:int=0; i < amt; i++)
{
value=trackids[i];
index=getItemIndexByProperty(myAC, "id", new String(value))
artistinfo[i]=myAC.getItemAt(index)
}
}
public function getItemIndexByProperty(array:ArrayCollection, property:String, value:String):Number
{
for (var i:Number=0; i < array.length; i++)
{
var obj:Object=Object(array[i])
if (obj[property].value == value)
return i;
}
return -1;
}