SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    what the heck is wrong with this sort?

    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:
    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);
    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
    Last edited by dmcalerney; May 11, 2005 at 08:32. Reason: elaboration

  2. #2
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is it a wack sort function?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •