if 0 {Richard Suchenwirth 2004-08-27 - In the Tcl chatroom, JPS discussed an extension to lsort which returns a list of indices in sorted order. This will go into a TIP, but for now here's a pure-Tcl implementation as proposed by James: }
proc lsort-indices list { if [llength $list] { set i -1 foreach e $list {lappend tmp [list [incr i] $e]} foreach e [lsort -index 1 $tmp] {lappend res [lindex $e 0]} set res } }
if 0 {Testing:
% lsort-indices {c b a} 2 1 0 % lsort-indices {} % lsort-indices {a a a} 0 1 2
RS removed the initialisations of tmp and res, but then had to guard the body with a one-armed if - it implies "else {}", which is the correct return value for an empty list.
Additional list functions | Arts and crafts of Tcl-Tk programming }