排序关联数组而不会丢失索引

时间:2012-06-06 12:37:06

标签: arrays actionscript-3

我有以下数组:

packages    Array (@24ff99c1)   
                [99767] Array (@24ff9809)   
                    forceUpload false   
                    length  0   
                    name    "testdocument.indd" 
                    packageId   "99767" 
                    progressStatus  "allowed"   
                    singleItemSize  50582294 [0x303d316]    
                    singleItemUploadedSoFar 17296352 [0x107ebe0]    
                    sortIndex   1338984578952 [0x137c1b33f88]   
                    status  "upload busy"   
                    totalSize   431203849 [0x19b3a609]  
                    uploadedAtm "37015918"  
                    uploadedSoFar   "19719566"  
                [94854] Array (@24ff7fd9)   
                    forceUpload false   
                    length  0   
                    name    "testdocument.indd" 
                    packageId   "16842" 
                    progressStatus  "allowed"   
                    singleItemSize  6344704 [0x60d000]  
                    singleItemUploadedSoFar 6344704 [0x60d000]  
                    sortIndex   1338984606428 [0x137c1b3aadc]   
                    status  "upload busy"   
                    totalSize   862407698 [0x33674c12]  
                    uploadedAtm "6344704"   
                    uploadedSoFar   ""  
                length  2   

现在我想根据sortIndex对它进行排序。我试着这样做:

packageData.data.packages.sortOn("sortIndex");

它已排序,但问题是我的索引变为0,1,2,......如下所示:

packages    Array (@24ff99c1)   
                [0] Array (@24ff9809)   
                    forceUpload false   
                    length  0   
                    name    "testdocument.indd" 
                    packageId   "99767" 
                    progressStatus  "allowed"   
                    singleItemSize  50582294 [0x303d316]    
                    singleItemUploadedSoFar 17296352 [0x107ebe0]    
                    sortIndex   1338984578952 [0x137c1b33f88]   
                    status  "upload busy"   
                    totalSize   431203849 [0x19b3a609]  
                    uploadedAtm "37015918"  
                    uploadedSoFar   "19719566"  
                [1] Array (@24ff7fd9)   
                    forceUpload false   
                    length  0   
                    name    "testdocument.indd" 
                    packageId   "16842" 
                    progressStatus  "allowed"   
                    singleItemSize  6344704 [0x60d000]  
                    singleItemUploadedSoFar 6344704 [0x60d000]  
                    sortIndex   1338984606428 [0x137c1b3aadc]   
                    status  "upload busy"   
                    totalSize   862407698 [0x33674c12]  
                    uploadedAtm "6344704"   
                    uploadedSoFar   ""  
                length  2

如何在不丢失索引的情况下对数组进行排序? 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

你不能。排序更改数组的顺序。只有你可以选择添加一个包含你希望它拥有的索引的数组项。

答案 1 :(得分:0)

这里的问题是你的数组键是整数。
因为它们是int,所以它充当常规数组而不是关联数组 当您执行包[99767]时,您正在查看索引为99767的元素 通过对数组进行排序,它将使您的索引开始为0.
您需要更改的是将键转换为字符串。