Sorting 2d array by members of 1st row | Newbie | | Join Date: Feb 2008
Posts: 3
| |
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. - #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
Any assistance appreciated!
Andy
|  | Moderator | | Join Date: Oct 2006 Location: New York, United States of America
Posts: 3,428
| | | 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: - struct two_nums {
-
int num1; // Sort by num1
-
int num2;
-
};
(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
| | | 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: - #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;
-
}
| | Newbie | | Join Date: Mar 2008
Posts: 2
| | | 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?
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,358 network members.
|