Connecting Tech Pros Worldwide Help | Site Map

Sorting 2d array by members of 1st row

Newbie
 
Join Date: Feb 2008
Posts: 3
#1: Feb 28 '08
Dear users,

I have just registered with this forum, and introduced myself as such:
http://www.thescripts.com/forum/thread776349.html

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.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int values[2][6] = {
  5.                 {4, 1, 10, 9, 2, 5 },
  6.                 {1, 2, 3, 4, 5, 6}
  7.                 };
  8.  
  9. int compare (const void * a, const void * b)
  10. {
  11.   return ( *(int*)a - *(int*)b );
  12. }
  13.  
  14. int main ()
  15. {
  16.  
  17.   int i;
  18.  
  19.   qsort (values, 6, sizeof(int), compare);
  20.  
  21.   for (i = 0; i < 6; i++)
  22.   {
  23.     printf ("%d ",values[0][ i ]);
  24.  
  25.   }
  26.   printf("\n");
  27.   for (i = 0; i < 6; i++)
  28.   {
  29.     printf("%d " , values[1][i]);
  30.   }
  31.   return 0;
  32. }
which returns
Expand|Select|Wrap|Line Numbers
  1. 1 2 4 5 9 10 
  2. 1 2 3 4 5 6 
Any assistance appreciated!
Andy
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#2: Feb 28 '08

re: Sorting 2d array by members of 1st row


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

Expand|Select|Wrap|Line Numbers
  1. struct two_nums {
  2.    int num1; // Sort by num1
  3.    int num2;
  4. };
(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.
Newbie
 
Join Date: Feb 2008
Posts: 3
#3: Feb 28 '08

re: Sorting 2d array by members of 1st row


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:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int values[2][6] = {
  5.                 {4, 1, 10, 9, 2, 5 },
  6.                 {1, 2, 3, 4, 5, 6}
  7.                 };
  8. int values_comp[2][6] = {
  9.                 {4, 1, 10, 9, 2, 5 },
  10.                 {1, 2, 3, 4, 5, 6}
  11.                 };
  12.  
  13. int compare (const void * a, const void * b)
  14. {
  15.   return ( *(int*)a - *(int*)b );
  16. }
  17.  
  18. int main ()
  19. {
  20.  
  21.   int i,j;
  22.  
  23.   qsort (values, 6, sizeof(int), compare);
  24.   for(i=0;i<6;i++){
  25.       for(j=0;j<6;j++){
  26.           if(values[0][i] == values_comp[0][j]){
  27.               values[1][i] = values_comp[1][j];
  28.               break;
  29.           }
  30.       }
  31.   }
  32.  
  33.   for (i = 0; i < 6; i++)
  34.   {
  35.     printf ("%d ",values[0][ i ]);
  36.  
  37.   }
  38.   printf("\n");
  39.   for (i = 0; i < 6; i++)
  40.   {
  41.     printf("%d " , values[1][i]);
  42.   }
  43.   return 0;
  44. }
Newbie
 
Join Date: Mar 2008
Posts: 2
#4: Mar 18 '08

re: Sorting 2d array by members of 1st row


Hello;

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