Skip to main content
2 of 2
added 242 characters in body
kglr
  • 400.5k
  • 18
  • 488
  • 929

You can get the row sums using {2} as the second argument of Total:

Total[{{1, 2}, {2, 0}, {3, 4}}, {2}]
{3, 2, 7}

Then you can use Ordering and Sort to get the ordering of sums and the sorted sums, respectively:

Through @ {Ordering, Sort} @ %
{{2, 1, 3}, {2, 3, 7}}

and Transpose the resulting pair of lists:

Transpose @ %
{{2, 2}, {1, 3}, {3, 7}}

Combine the three steps to define a function:

ClearAll[indexedSortedSum]
indexedSortedSum = Transpose[Through@{Ordering, Sort}@Total[#, {2}]] &;

indexedSortedSum @ {{1, 2}, {2, 0}, {3, 4}}
{{2, 2}, {1, 3}, {3, 7}}

Alternatively, you can Apply Plus at Level 1 instead if using Total

ClearAll[indexedSortedSum2]
indexedSortedSum2 = Transpose[Through@{Ordering, Sort}[Plus @@@ #]] &;

indexedSortedSum2 @ {{1, 2}, {2, 0}, {3, 4}}
{{2, 2}, {1, 3}, {3, 7}}
kglr
  • 400.5k
  • 18
  • 488
  • 929