Version 5 of lsort index vector

Updated 2004-09-13 02:37:02

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 (TIP#217) but for now, here's a pure-Tcl implementation: }

 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.


Lars H had this solution, which is said to be up to a third faster:

 proc lsort-indices itemL {
    set pairL [list]
    foreach item $itemL {
       lappend pairL [list $item [llength $pairL]]
    }
    set indexL [list]
    foreach pair [lsort -index 0 $pairL] {
       lappend indexL [lindex $pair 1]
    }
    return $indexL
 }

Additional list functions | Arts and crafts of Tcl-Tk programming }