Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sorting Parallel Arrays

    How do I modify this code so that it rearranges the letters corresponding to their original positions as they related to the numbers? Right now it just prints the letters from a-h after I sort the numbers. The original index position of 15 corresponds the index of h. So when array a is sorted in ascending order, it should display 15H instead of 15B. I can't figure it out how to swap array abc at the same time (if that's what I need to do).
    Code:
       var abc = "ABCDEFGH";
       var a = new Array(12,23,34,65,21,37,99,15);           
       document.write( "Original Number Array: "+a+"<br><br> new pairs: ");
                 
     function swap( arr, x, y )  {
      	var temp = arr[x];
      	arr[x] = arr[y];
      	arr[y] = temp;
    
         }
         
     for ( var f = 0; f < a.length-1; f++ ) {
        		var iSmallest = f;
        		for ( var i = f+1; i < a.length; i++ )
         			if ( a[i] < a[iSmallest] )
            			iSmallest = i;
    
        		swap( a, f, iSmallest ); 						
      	}
     for ( var i = 0; i < abc.length; i++ )
        for ( var i = 0; i < a.length; i++ )
        	     document.write( "  " + a[i]+abc[i]/*this i know is wrong*/);

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Why not have a "2D" array and then use the built-in Array sort function?
    Code:
    var arr = new Array(
      [12, 'A'],
      [23, 'B'],
      [34, 'C'],
      [65, 'D'],
      [21, 'E'],
      [37, 'F'],
      [99, 'G'],
      [15, 'J']
    );           
    
    function compareFunc(a, b){
      return a[0] - b[0];
    }
    
    //sort it
    arr.sort(compareFunc);   
    
    for (var i=0; i<arr.length; i++){
      alert(arr[0] + "=" + a[1]);
    }

  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I can't get that to work. The error console just says A is not defined.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Looks like this thread is related to this thread. If so, then the solution should be like this:
    Code:
    <script type="text/javascript">
    
    function compareFunc(a, b){ //ascending order
      return a[0] - b[0];
    }
    
    var arr = new Array();
    for (var i=0; i<26; i++){
      arr[i] = [0, String.fromCharCode(i+65)];
    }
    
    function countLetter(letters){
      letters = letters.toUpperCase();
      for (var p = 0; p < letters.length; p++) {
        arr[letters.charCodeAt(p)-65][0]++;
      }
      arr.sort(compareFunc);
    }
    
    var input = prompt("enter string", "");
    countLetter(input);
    var s="";
    for (var i=0; i<arr.length; i++){
      if (arr[i][0] != 0){ //don't display 0's
        s += arr[i][1] + "=" + arr[i][0] + "\n";
      }
    }
    alert(s);
    
    </script>

  • #5
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The output isn't sorted numerically (if I typed abbccc output should be 3=c, 2=b, 1=a).

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Can you post your code? When I run my code and typed abbccc, the output was:
    Code:
    A=1
    B=2
    C=3
    It is sorted in ascending order. To do it the reverse, you need to change this:
    Code:
    function compareFunc(a, b){ //ascending order
      return a[0] - b[0];
    }
    to:
    Code:
    function compareFunc(a, b){ //descending order
      return b[0] - a[0];
    }

  • #7
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh crap. I was kinda spaced out. My mistake! I do know the definition of ascending order, btw!

  • #8
    Regular Coder Actinia's Avatar
    Join Date
    Feb 2007
    Location
    Essex, UK
    Posts
    250
    Thanks
    1
    Thanked 23 Times in 22 Posts
    Why not just swap the two arrays in parallel:

    Replace:
    Code:
     
    swap( a, f, iSmallest );
    with:
    Code:
     
    swap( a, f, iSmallest ); swap( abc, f, iSmallest );


  •  

    Posting Permissions

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