Sorting twodimensional Array in AS3
So, i have a two-dimensional Array of ID's and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count
I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array - sortArray.
Then when i diplay the results i plan on using the data from sort array to find out what color the voteArray data shou开发者_如何学JAVAld have. The data from voteArray should be in correct order by ID.
SO this is what I do:
sortArray = voteArray;
sortArray.sortOn("1",Array.NUMERIC);
This messes up the sorting of the data in voteArray. What am I doing wrong?
public function mySort(a:Array, b:Array):Number {
if(a[1] <= b[1]) {
return 1;
}
return -1;
}
sortArray.sort(mySort);
if you say:
sortArray = voteArray;
You are only assigning the reference of voteArray to sortArray. So after that statement both your variables are pointing to the same piece of memory.
I'm a bit surprised the Array class in flash does not have a clone function or a copy constructor
ways you can copy an array:
var sortArray:Array = voteArray.filter(function(){return true;});
var sortArray:Array = voteArray.slice(0);
精彩评论