The easiest way to sort by the second element is to define your own sort function, which could work as follows:
fun({KeyA,ValA}, {KeyB,ValB}) -> {ValA,KeyA} =< {ValB,KeyB} end.
And name it in lists:sort/2 :
1> lists:sort(fun({KeyA,ValA}, {KeyB,ValB}) -> {ValA,KeyA} =< {ValB,KeyB} end., [{a,b},{b,a},{b,b}]). [{b,a},{a,b},{b,b}]
This is because Erlang will always automatically compare tuples from the first to the last element. This function changes the first and second elements so that the second acts as the first comparison point. Then the key in your dict will be used to order records where the values ββare the same.
source share