大家好,我需要对一个数组集合进行排序。问题是我的数组是由两个不同的数组组合而成的,在一个数组中,age字段被称为" age ",在另一个数组中,age字段被称为"MYAGE“。
现在我需要根据年龄对组合后的数组进行排序,但我得到了两个名称不同的字段。我可以遍历数组并将所有"MYAGE“更改为"AGE”,但我想知道是否有一种方法可以在当前状态下对数组进行排序?
感谢前面的
发布于 2011-08-10 19:37:03
假设您有一个名为myCollection的ArrayCollection。我希望下面的内容能解决你的这个问题:
private function compareItems(a:Object, b:Object, fields:Array = null):int
{
var firstValue:Number = "AGE" in a ? a["AGE"] : a["MYAGE"];
var secondValue:Number = "AGE" in b ? b["AGE"] : b["MYAGE"];
if (firstValue == secondValue)
return 0;
return firstValue > secondValue ? 1 : -1;
}
…
var sort:ISort = new Sort();
sort.compareFunction = compareItems;
myCollection.sort = sort;
myCollection.refresh();https://stackoverflow.com/questions/7009979
复制相似问题