I have a 2-d array, containing only 0 or 1.
I want to sort it in descend order on rows (no change on each column) using STL sort algorithm. but I don’t know how to pass the parameter and how to write the compare function in sort(first, last, comp);
like:
0 1 1 1
1 1 0 1
1 0 1 0
would be sorted like this:
1 1 0 1
1 0 1 0
0 1 1 1
my data structure is like this:
int **table = 0;
table = new int *[row];
for(int i=0;i<row;i++)
table[i] = new int[column];
I can only write the sort function like this:
sort(a[0], a[0]+row, compare_function);
bool compare_function(int a[], int b[])
{
int i =0;
while(a[i]==0 ||a[i]==1)
{
if(a[i]>b[i])
return true;
else
i++;
}
return false;
}
But it doesn’t work. Can some one help me?
Thank you very much.
Your call to sort looks wrong to me (though you never said what
ais). It should besort(table, table+row, compare_function)But I’d do it a bit differently anyway (
std::lexicographical_comparecomes from<algorithm>):And use it like: