2D Array sorting?

how could i output the sum of the array rows
on the screen in form of biggest row sum to smallest row sum.?

for exampel we have this 2D Array :

3 2 3
1 1 1
2 2 2

the output according to the row sum is:
1 1 1
2 2 2
3 2 3


is it clear?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
struct Row{ //this could be a template class
   int r[3];
   //you could overload operator [] here
};

bool Less(Row a, Row b){
   return a.r[0]+a.r[1]+a.r[2] < b.r[0]+b.r[1]+b.r[2];//you should use a loop
}

//...
Row array[3] = { {3, 2, 3}, {1, 1, 1}, {2, 2, 2} };//you can do that as long as Row has no ctor

std::sort(array, array+3, Less);//from <algorithm>. you can write one yourself too. just use the Less to compare 
Topic archived. No new replies allowed.