views:

267

answers:

2

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 should 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?

A: 

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);
Les
Thanks! Now I only have one problem...sortArray.sortOn("1", Array.DESCENDING | Array.NUMERICAL);returns this:6,1 2,1 3,1 4,15,21,57,18,29,110,1
Frode
i'm not sure but sortOn expects an object, not an array index. so if you changed your datastructure to voteArray[i] = {"id":2, "voteCount":2} turning it in an object, then calling voteArray.sortOn("voteCount",...) it should work.
Les
+1  A: 
bhups
+1 for a concise proper solution. dont forget the `var sortArray:Array = voteArray.slice(0);` though. documentation - http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Array.html#sort%28%29
greg