Blid - 1 year ago 31

Python Question

The exact same question is answered here but in MATLAB.

My question is this: Given a matrix, sort it in the ascending order according to the sum of its rows.

That is, if

`A`

`A = [[9, 8, 7],`

[2, 5, 7],

[1, 3, 4]]

therefore, I would get:

`B = [[1, 3, 4],`

[2, 5, 7],

[9, 8, 7]]

because the sum of the 1st row of

`A`

`24`

`A`

`14`

`A`

`8`

`B`

`A`

`B`

`A`

`B`

`A`

I am looking for a solution that uses built-in function (if possible). I am not looking for an algorithm for this.

Answer

There is a function available that does the trick:

```
sorted(A, key=sum)
```

gives you the desired output:

```
[[1, 3, 4], [2, 5, 7], [9, 8, 7]]
```