Sorting Two Dimensional Array

Is there a way to sort a two dimensional array by row?

For example i would want to sort in ascending order between:
array[0][4]
array[1][4]
array[2][4]

So if the array was as follows:
3 8 7 2
9 12 0 4
12 2 14 1

it will sort to:
2 3 7 8
0 4 9 12
1 2 12 14

Any help would be greatly appreciated. Thanks!
There's no such thing as a two dimensional array.

Make your array like this
int array[hSize*vSize]
And access like this:
array[vSize*y+x]

If you want to sort in rows, let your algo march x
0<=x<vSize

In columns, let it march y
0<=y<hSize
and access as above.
JoR wrote:
There's no such thing as a two dimensional array.

Of course there is. The syntax the OP used is correct.

@tylerd213: just sort each part of the array at a time, as if they were separate arrays, like this:

1
2
for(int i = 0; i < 4; ++i)
    std::sort(&arr[i][0], &arr[i][4]);

Thank you Filipe :)
That worked great!
Topic archived. No new replies allowed.