what the heck is wrong with this sort

  • dmcalerney
  • Born
  • Born
  • dmcalerney
  • Posts: 1
  • Loc: arcata, ca

Post 3+ Months Ago

Howdy folks.

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?

CoDE TO FOLLOW
Thanks,
Danny

Code: [ Select ]
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];
     }
   }
}

trace("out_array: " add out_array);
  1. function sortByNumber(a, b)
  2. {
  3.    return(a < b);
  4. }
  5. sort_array = new Array();
  6. out_array = new Array();
  7. cmp_name_array = new Array();
  8. orig_array = new Array();
  9. temp_array = new Array();
  10. orig_array = [3, 5, 1, 2, 4];
  11. trace("orig_array before sort: " add orig_array);
  12. cmp_name_array = ["graph3", "graph5", "graph1", "graph2", "graph4"];
  13. temp_array = orig_array;
  14. sort_array = temp_array;
  15. trace("sort_array before sort: " add sort_array);
  16. trace("temp_array before sort: " add temp_array);
  17. sort_array.sort(sortByNumber);
  18. trace("cmp_name_array: " add cmp_name_array);
  19. trace("orig_array after sort: " add orig_array);
  20. trace("sort_array after_sort: " add sort_array);
  21. trace("temp_array after sort: " add temp_array);
  22. out_array = new Array();
  23. trace("orig_array length: " add orig_array.length);
  24. for(t=0; t< orig_array.length; t++)
  25. {
  26.   
  27.    temp = orig_array[t];
  28.    for(j=0; j< sort_array.length; j++)
  29.    {
  30.      
  31.      if(temp == sort_array[j])
  32.      {
  33.         out_array[j] = cmp_name_array[t];
  34.      }
  35.    }
  36. }
  37. trace("out_array: " add out_array);


Oh yeah, here's the output

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
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

Code: [ Select ]
temp_array = orig_array


That doesn't copy the array. Any changes made to orig_array will just be reflected in temp_array (and vice versa) from then on.

When dealing with copying basic arrays (meaning 1 dimensional as you are using) I generally use this method..

Place this before your code (it defines a function to copy an array, only needs to be defined once, and it needs to be defined before it is used... also since it is a prototype it is a global function)
Code: [ Select ]
Array.prototype.copy = Array.prototype.slice;


Useage would be (in your case)...

Code: [ Select ]
temp_array = orig_array.copy();

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 14 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.