[ACCEPTED]-Sort Tcl dict by value-tcl

Accepted answer
Score: 13

There is, if you have Tcl 8.6 (this uses 5 the fact that dictionaries can be converted 4 cheaply to and from lists):

set sorted [lsort -integer -stride 2 -index 1 $d1]

If you're still 3 on 8.5 (likely; 8.6 is still in beta) then 2 you need to use several steps:

proc sortDictByValue {dict args} {
    set lst {}
    dict for {k v} $dict {lappend lst [list $k $v]}
    return [concat {*}[lsort -index 1 {*}$args $lst]]
}
set sorted [sortDictByValue $d1]

The -stride option 1 is easier to use, if you've got it.

More Related questions