Sorting 2d array by members of 1st row

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ajb1983
    New Member
    • Feb 2008
    • 3

    Sorting 2d array by members of 1st row

    Dear users,

    I have just registered with this forum, and introduced myself as such:


    My question regards a 2d array, to be sorted according to the first row, such that an array such as
    {4,6,3,7,2},{1, 2,3,4,5} would be sorted to
    {2,3,4,6,7},{5, 3,1,2,4}
    I have taken the following example code from an FAQ on the internet, but cannot make it work as intended.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int values[2][6] = {
    				{4, 1, 10, 9, 2, 5 },
    				{1, 2, 3, 4, 5, 6}
    				};
    
    int compare (const void * a, const void * b)
    {
      return ( *(int*)a - *(int*)b );
    }
    
    int main ()
    {
    
      int i;
      
      qsort (values, 6, sizeof(int), compare);
      
      for (i = 0; i < 6; i++)
      {
        printf ("%d ",values[0][ i ]);
      
      }
      printf("\n");
      for (i = 0; i < 6; i++)
      {
    	printf("%d " , values[1][i]);
      }
      return 0;
    }
    which returns
    Code:
    1 2 4 5 9 10 
    1 2 3 4 5 6
    Any assistance appreciated!
    Andy
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    You should probably use a struct for this. If you want the two numbers paired, define a struct:

    [CODE=c]struct two_nums {
    int num1; // Sort by num1
    int num2;
    };[/CODE]

    (I'm not 100% on the actual C syntax for declaring/defining structs in C, but you can easily find that information with a Google search).

    Now your compare function will treat it's void* arguments as two_nums* arguments, and compare based on a.num1 and b.num1. This will then switch the entire struct (both numbers) and keep them paired together.

    The other way would be to write your own sort function involving a swap function. Inside swap, switch both the values in the first array and the values in the second array. This will be messy, and you'll have to get your own sort function working rather than using the built in qsort function.

    I suggest the struct solution, but both will work.

    Comment

    • ajb1983
      New Member
      • Feb 2008
      • 3

      #3
      Yes, thanks for that Ganon11.

      I will probably try the struct method once I have time since it seems neat.
      In the meantime, I had actually blundered through with the following, which works:

      Code:
      #include <stdio.h>
      #include <stdlib.h>
      
      int values[2][6] = {
      				{4, 1, 10, 9, 2, 5 },
      				{1, 2, 3, 4, 5, 6}
      				};
      int values_comp[2][6] = {
      				{4, 1, 10, 9, 2, 5 },
      				{1, 2, 3, 4, 5, 6}
      				};
      
      int compare (const void * a, const void * b)
      {
        return ( *(int*)a - *(int*)b );
      }
      
      int main ()
      {
      
        int i,j;
        
        qsort (values, 6, sizeof(int), compare);
        for(i=0;i<6;i++){
        	for(j=0;j<6;j++){
        		if(values[0][i] == values_comp[0][j]){
        			values[1][i] = values_comp[1][j];
        			break;
        		}
        	}
        }
        
        for (i = 0; i < 6; i++)
        {
          printf ("%d ",values[0][ i ]);
        
        }
        printf("\n");
        for (i = 0; i < 6; i++)
        {
      	printf("%d " , values[1][i]);
        }
        return 0;
      }

      Comment

      • snowdrop
        New Member
        • Mar 2008
        • 2

        #4
        Hello;

        I have the same problem in C#, I want to sort 2D array according to the first row .. could anyone help me?

        Comment

        Working...