The INITIAL_ARRAY is
Key -> Value
B      8
C     10
A      5
E      3
D      1
To get a sorted array based on key, I use
set sorted_keys_array [lsort [array names INITIAL_ARRAY]]
to get the output
Key -> Value
A     5
B     8
C     10
D     1
E     3
Like wise, how to get a sorted tcl array based on values of keys, like output below?
Key -> Value
 C     10
 B     8 
 A     5
 E     3
 D     1
                Starting with Tcl 8.6, you could do
lsort -stride 2 -integer [array get a]
which would produce a flat list of key/value pairs sorted on values.
Before lsort gained the -stride option, you had to resort to constructing a list of lists out of the flat list array get returns and then sort it using the -index option for lsort:
set x [list]
foreach {k v} [array get a] {
    lappend x [list $k $v]
}
lsort -integer -index 1 $x
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With