I want to compare a 2D array with my custom compare function. I also reference some similar problem, but it doesn't work.
Following is what I did (compare 2D array with row and I need to use array instead of vector
):
int n, dim;
int box[33][33];
bool cmp(const int a[], const int b[]){
for(int i = 0; i < dim; i++){
if(a[i] != b[i])
return a[i] < b[i];
}
return a[dim] < b[dim];
}
int main()
{
while(cin>>n>>dim){
for(int i = 0; i < n; i++){
for(int j = 0; j < dim; j++){
cin>>box[i][j];
}
box[i][dim] = i+1; // store index of box
sort(box[i], box[i]+dim);
}
sort(box, box + n, cmp); // This line is where I want to modify
for(int i = 0; i < n; i++){
for(int j = 0; j < dim; j++){
cout<<box[i][j]<<" ";
}
cout<<"\n";
}
}
return 0;
}