sepoto
January 29, 2011, 11:24pm
1
My language is C++. I have a multidimensional vector that I would like to sort by a specific "cell" or "field" within the main vector. Does anyone have any information on how to do this? I have searched all over the internet and every reference manual I can find. So far I have found very little to no information on this topic.
Thank you
homeboy
January 30, 2011, 12:33am
2
seems difficult....I wrote a program to illustrate the situation of a two dimensions array,more dimensions will be more difficult to implement.
typedef struct {
int a;
int b;
}EMBED;
bool my_sort(EMBED a,EMBED b){
return a.b > b.b;
}
int main()
{
int a[10][2];
EMBED embed_a[10];
srand(time(NULL));
for(int i=0;i<10;i++)
for(int j=0;j<2;j++)
a[j] = rand()%200;
for(int i=0;i<10;i++){
embed_a.a = a[0];
embed_a.b = a[1];
}
vector<EMBED> vec(embed_a,embed_a+10);
sort(vec.begin(),vec.end(),my_sort);
for(int i=0;i<10;i++){
for(int j=0;j<2;j++)
cout<<a[j]<<" ";
cout<<endl;
}
cout<<"============AFTER SORTING==============="<<endl;
for(vector<EMBED>::const_iterator iter = vec.begin();
iter != vec.end();iter++)
cout<<iter->a<<" "<<iter->b<<endl;
return 0;
}
jgt
January 30, 2011, 9:41am
3
Binary sort of a table Fortran V.
Sort table on column 2 then 1.
The order of equal sort keys is not predictable.
10 implicit integer (A-Y)
11 dimension sxlist (8,P2)
12 X0=P2 #P2=1+number of entries
13 10 X0=X0/2
14 if (X0) 15,70,15
15 15 X1=P2-X0
16 X2=1
17 20 X3=X2
18 30 X4=X3+X0
19 if (sxlist(2,X3)-sxlist(2,X4)) 60,45,40
20 45 if (sxlist(1,X3)-sxlist(1,X4)) 60,60,40
21 40 do 50 I-1,8
22 X5=sxlist(I,X3)
23 sxlist(I,X3)=sxlist(I,X4)
24 sxlist(I,X4)=X5
25 50 continue
26 X3=X3-X0
27 if (X3-1) 60,60,30
28 60 X2=X2+1
29 if (X2-X1) 20,20,10
30 70 return