I have associations where the values are nested lists:

assoclist= <|KeyA -> {{a,b,2},{c,d,4},{e,f,3}}, 
KeyB -> {{a,b,1},{c,d,4},{e,f,6}}, KeyC -> {{a,b,2},{c,d,6},{e,f,9}}|>;

Now I need to sort the associations by the total sum of the third elements (here the numbers) in each list so that Key C comes first (total sum = 17), than Key B (11) and Key A (9).

How can I do this?

  • 5
    $\begingroup$ SortBy[assoclist, -Total[#[[All, 3]]]&]? (Or use ReverseSortBy and get rid of the minus sign) $\endgroup$ Commented Aug 30, 2019 at 10:53
  • $\begingroup$ @SjoerdSmit: I have tried this before but it did not work. It must have been a typing error or so. However now it works perfectly, thx! $\endgroup$
    – M.A.
    Commented Aug 30, 2019 at 11:08

3 Answers 3


You may use ReverseSortBy with Total and Composition.


Hope this helps.

  • $\begingroup$ Beautiful use of the Wolfram Language. The absence of specific list indices is very nice. It is also easily extended by changing Last or Total as needed. $\endgroup$
    – Lee
    Commented Aug 30, 2019 at 22:19


assoclist[[Reverse[Ordering[Total[assoclist[[All, All, 3]]]]]]]

With Transpose:

assoclist // 
  Map[Transpose] // 
  ReverseSortBy[Total@*Last] // 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.