根据 Flex 中数组的值对数组集合进行排序
我一直在研究对数组集合进行排序。到目前为止,我已经看到了诸如按升序和降序进行数字排序之类的事情。我正在寻找的是发现根据另一个数组中值的顺序对数组集合进行排序的方法。
例如:
我有一个包含 10 个数值的数组。我也有一个数组集合。每个 arracycollection 条目的属性之一对应于前一个数组中的值之一。
我希望能够根据数组中值的顺序对数组集合进行排序。
最好的方法是什么?
我正在考虑循环第一个数组,获取第一个值,在 arraycollection 中查找具有该值的条目,然后将其添加到新的 arraycollection 中,但这似乎有点啰嗦,我希望可能有一种聪明的方法来做到这一点它。
编辑
这是我到目前为止所拼凑的内容。虽然看起来有点太圆了
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我现在将添加我的编辑作为答案。
对于通过搜索到达这里的人来说可能会有用
如果有人可以提供更简洁或更好的方法,请发帖!
I'm going to add in my edit as the answer for now.
It may be useful for somebody who lands here from a search
If anybody can offer a more concise or better way please post!