简体   繁体   English

与Actionscript 3搭配

[英]arsort with Actionscript 3

How would one sort an indexed array and maintain the index association in Actionscript 3.0. 如何在Actionscript 3.0中对索引数组进行排序并维护索引关联。 The Array.sort(); Array.sort(); method seems to reindex the array no matter what. 方法似乎无论如何都会重新索引数组。 Basically I need to recreate the arsort php function in Actionscript. 基本上,我需要在Actionscript中重新创建arsort php函数。 Possible? 可能?

To do so you would need an associative array (ie an Object ) because when the indexes are integers flash would automatically reorder them from 0 to n as you may have noticed (This can be quite annoying in your case, but has its reasons). 为此,您将需要一个关联数组(即Object ),因为当索引为整数时,如您可能已经注意到的那样,flash会自动将它们从0重新排序为n (这可能会很烦人,但有其原因)。

If you want to keep track of the key values, a hack is to store the indexes in the value for each item in the array : 如果要跟踪键值,则可以将索引存储在数组中每个项目的值中:

var array:Object = new Array();

array.push({index:0,name:"Tom"});
array.push({index:1,name:"Andrew"});
array.push({index:2,name:"Mark"});

array.sortOn("name");

for each(var item:Object in array)
    trace(item.index, item.name);

This will trace : 这将跟踪:

1 Andrew 
2 Mark 
0 Tom

nb This can be improved if your values are strong typed in a Vector instead of an Array . nb如果在Vector而不是Array中强类型键入值,则可以改进此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM