I’m trying to sort a small array. The problem here is that the sort function is sorting the array it’s applied to PLUS another array that is just hanging out next to it. It’s weird. Anyone know what’s happening here?
function sortByNumber(a, b)
{
return(a < b);
}
sort_array = new Array();
out_array = new Array();
cmp_name_array = new Array();
orig_array = new Array();
temp_array = new Array();
orig_array = [3, 5, 1, 2, 4];
trace("orig_array before sort: " add orig_array);
cmp_name_array = ["graph3", "graph5", "graph1", "graph2", "graph4"];
temp_array = orig_array;
sort_array = temp_array;
trace("sort_array before sort: " add sort_array);
trace("temp_array before sort: " add temp_array);
sort_array.sort(sortByNumber);
trace("cmp_name_array: " add cmp_name_array);
trace("orig_array after sort: " add orig_array);
trace("sort_array after_sort: " add sort_array);
trace("temp_array after sort: " add temp_array);
out_array = new Array();
trace("orig_array length: " add orig_array.length);
for(t=0; t< orig_array.length; t++)
{
temp = orig_array[t];
for(j=0; j< sort_array.length; j++)
{
if(temp == sort_array[j])
{
out_array[j] = cmp_name_array[t];
}
}
}
Here’s the output for this:
orig_array before sort: 3,5,1,2,4
sort_array before sort: 3,5,1,2,4
temp_array before sort: 3,5,1,2,4
cmp_name_array: graph3,graph5,graph1,graph2,graph4
orig_array after sort: 5,4,3,2,1 //this should NOT be sorted
sort_array after_sort: 5,4,3,2,1 //this SHOULD be sorted
temp_array after sort: 5,4,3,2,1
orig_array length: 5
out_array: graph3,graph5,graph1,graph2,graph4
trace("out_array: " add out_array);