Swapping rows in a 2D array
So let's say I have a 3x4 array filled with these values:
15 7.5 1 15
12 6 1 12
23 11.5 1 23
Now I want to swap rows so that the row with the largest value in the first column is on top and next largest value is in the second row
So I'd end up with this
23 11.5 1 23
15 7.5 1 15
12 6 1 12
Question is.. Is there a way to deal with an entire row of a 2D array at once? Or do I have to sort them one colum at a time?
Here's sort of what I have going now. It swaps the values in the first column fine but leaves the rest of the columns alone. I can do the same thing for the rest of the colums, but I was just wondering if there was an easier way to do it..
Code:
for( i = 0; i < 3; i++)
{
//Set initial value as the current max
index_of_max = i;
max_value = h[i][0];
for( j = i + 1; j < 3; j++)
{
if( h[j][0] > max_value)
{
max_value = h[j][0];
index_of_max = j;
}
}
//Swap h[i][0] and max value
if( index_of_max != i)
{
tmp = h[i][0];
h[i][0] = h[index_of_max][0];
h[index_of_max][0] = tmp;
}
}