Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Tcl/Tk Bubblesort

Posted by Andy (Andy), 21 January 2003
Do you guys know if there is anything like a Bubblesort in Tcl/Tk ??

I have the problem that I have a table with a few columns and I want to reach that I have to press a button and thereafter it's just sorting column 2 (A-Z) and no other column. The other columns shouldn't be touched.

Does anybody know how to accomplish that ?

Posted by admin (Graham Ellis), 21 January 2003
The "standard" answer is that in Tcl, the structure you sort is a list using the lsort command (and it doesn't actually matter is it uses a bubble sort, a quick sort or anothersort algorithm ).  However, you're referring to tables and columns so I suspect you have some SQL mixed up in this, or are you looking to sort a two dimensional array?   Do post a bit more of the background and I'll come back with a more tuned answer!

Posted by Andy (Andy), 22 January 2003
Already thnx for the help Graham.

Well.. here's the code :

set BA [list ] ;

proc ladedatei Datei1 {      
 
  global BA   ;
  set Datei [open $Datei1 ]      
  while {[gets $Datei Zeile ] >= 0 } {
  set b [string match BA*  $Zeile ]        
 
  if {$b == 1} { set e [string range $Zeile 7 end]                  
  lappend BA $e}  ;


}

}
ladedatei c:\\temp\\eingabe02.dat


set a [llength $BA1]    
for {set index 0} {$index <= $a } {incr index } {      
set c [lindex $BA1 $index]              
.f1.text insert end $c  ;# .f1.text = Listbox              
}


....now theres the following list :

BA1 {bla1  bla2    bla3  bla4    bla5}
     {bla1  bla2    bla3  bla4    bla5}
     {bla1  bla2    bla3  bla4    bla5}

... and I want to sort the single columns without affection the the other ones.

It's no array, just a list.

Hope you can help me with this

Posted by admin (Graham Ellis), 23 January 2003
Thanks for the further explanation.  You're looking to sort all the elements you have marked as "bla2" within your list of lists, but leave all the "bla1", "bla3" etcs in exactly the place they're at?

OK - somewhat unusual requirement.  It would be much more common to want to sort the complete sub-lists (rows as I think you described them earlier, and how they're layed out in your post) and that woulod be easier.  The built in lsort re-orders the elements of a list but leaves the contents of each list element unchanged - your requirement is to change the contents of each member of the list as you go, so it's more than just a sort ... and there's not a quick, single line answer to your question.

Scheme to sort:

1. Loop through each of the rows, extracting the "bla2" fields into a separate list

2. Sort that separate list with lsort

3. Loop through each of the rows of the incoming list again,
replacing the bla2 incoming value with the next one from the sorted list, and appending the resulting sublist onto the structure you finally want.

Your still using the built in lsort   - it's just that you have a bit more data management to do beforehand and after

Posted by Andy (Andy), 27 January 2003
Thanks a lot, that was a good hint.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho