Originally Posted by
brewbuck
You said you wanted to sort by z. Then, within each group of z, you wanted to sort by time. That's what this comparison function accomplishes. Unless I misunderstand what you are trying to do, this is correct.
You do not need to sort more than once.
Did you even try it?
Yes i tried it. It didn't work right:
Code:
Data SortedThe amount in each z bin:
7, 3
time, z, y, x
1.000000, 1.000000, 2.000000, 15.000000
5.000000, 1.000000, 2.000000, 5.000000
6.000000, 11.000000, 22.000000, 5.000000
10.000000, 11.000000, 52.000000, 5.000000
3.000000, 13.000000, 2.000000, 45.000000
7.000000, 15.000000, 42.000000, 5.000000
8.000000, 51.000000, 2.000000, 5.000000
4.000000, 111.000000, 32.000000, 5.000000
2.000000, 122.000000, 2.000000, 25.000000
9.000000, 221.000000, 2.000000, 5.000000
This is my output. Z is sorted which I have done but it still doesnt sort by time for the two Z bin.
SO by trying this way:
Code:
/* first bin */
for (i = (a - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (compare(tmp.z[j-1], tmp.t[j-1], tmp.z[j], tmp.t[j]) == 1)
{
temp3 = tmp.t[j-1];
tmp.t[j-1] = tmp.t[j];
tmp.t[j] = temp3;
ss1 = tmp.x[j-1];
tmp.x[j-1] = tmp.x[j];
tmp.x[j] = ss1;
ss2 = tmp.y[j-1];
tmp.y[j-1] = tmp.y[j];
tmp.y[j] = ss2;
ss3 = tmp.z[j-1];
tmp.z[j-1] = tmp.z[j];
tmp.z[j] = ss3;
}
}
}
/* second bin */
for (i = (b - 1); i >= 0; i--)
{
for (j = (a); j <= i; j++)
{
if (compare(tmp.z[j-1], tmp.t[j-1], tmp.z[j], tmp.t[j]) == 1)
{
temps = tmp.t[j-1];
tmp.t[j-1] = tmp.t[j];
tmp.t[j] = temps;
sss1 = tmp.x[j-1];
tmp.x[j-1] = tmp.x[j];
tmp.x[j] = sss1;
sss2 = tmp.y[j-1];
tmp.y[j-1] = tmp.y[j];
tmp.y[j] = sss2;
sss3 = tmp.z[j-1];
tmp.z[j-1] = tmp.z[j];
tmp.z[j] = sss3;
}
}
}
printf("Sorting complete \n");
fprintf(ofp,"Data Sorted");
fprintf(ofp,"The amount in each z bin:\n");
fprintf(ofp,"%d, %d\n", a, b);
fprintf(ofp,"time, z, y, x \n");
for (i = 0; i < a; ++i)
fprintf(ofp,"%lf, %lf, %lf, %lf \n", tmp.t[i], tmp.z[i],tmp.y[i], tmp.x[i]);
fprintf(ofp,"***************\n");
for (i = a; i < al; ++i)
fprintf(ofp,"%lf, %lf, %lf, %lf \n", tmp.t[i], tmp.z[i],tmp.y[i], tmp.x[i]);
Reason for this way is I wanted the output to be like:
Code:
Data SortedThe amount in each z bin:
7, 3
time, z, y, x
1.000000, 1.000000, 2.000000, 15.000000
3.000000, 13.000000, 2.000000, 45.000000
5.000000, 1.000000, 2.000000, 5.000000
6.000000, 11.000000, 22.000000, 5.000000
7.000000, 15.000000, 42.000000, 5.000000
8.000000, 51.000000, 2.000000, 5.000000
10.000000, 11.000000, 52.000000, 5.000000
***************
4.000000, 111.000000, 32.000000, 5.000000
2.000000, 122.000000, 2.000000, 25.000000
9.000000, 221.000000, 2.000000, 5.000000
Minus the second bin not being sorted by t