Reputation: 399
I am using a procedure in Tcl to count the number items for a given variable. I am using an array to 'store' the initial counts. From there, I append them to a list (as I have read and learned that lists are much easier manipulate and work with in Tcl).
I want to sort my count list by the second number from largest to smallest. I have:
$ns at finalTime {
for {set i 0} {$i < [llength $subClusters]} {incr i} {
puts "Member count $i: $count($i)"
lappend countList "$i $count($i)"
}
puts $countList
puts [lsort -decreasing -index 1 $countList]
#set $countList [lsort -decreasing -index 1 $countList]
}
$ns at finalTime {
puts $countList
}
However, I get a very strange output on some runs:
# Original list:
{0 11} {1 3} {2 7} {3 13} {4 9} {5 2} {6 9} {7 8} {8 11} {9 10} {10 10} {11 3} {12 3} {13 12} {14 12} {15 12} {16 1} {17 10} {18 3} {19 9}
# Should be sorted list
{4 9} {6 9} {19 9} {7 8} {2 7} {1 3} {11 3} {12 3} {18 3} {5 2} {3 13} {13 12} {14 12} {15 12} {0 11} {8 11} {9 10} {10 10} {17 10} {16 1}
The lsort
list will be correct on roughly 20% runs. Why am I getting such a strange output on the others? It seems that it resets itself in the middle of sorting and goes back to largest 2nd integer and goes down from there. Another hint, it falls apart when the 2nd integer is greater than 10 or 2 digits long. Am I referencing something wrong? I do not understand why it works sometimes and not others.
I need to take the re-ordered list and feed it into another procedure.
Upvotes: 0
Views: 397
Reputation: 4813
By default lsort
performs an ascii sort. Because '9' appears later in the ascii table than '1', 9 is considered higher than 13. If you want to compare the numbers as integers, you have to tell that to lsort using the -integer option:
puts [lsort -integer -decreasing -index 1 $countList]
Upvotes: 3