Posted by susyq78 (susyq78), 31 August 2003Hi - I'm hoping someone can help me...
I'm using a Mclistbox and want to sort on a particular column. I have code to sort and it works except that in the column if there are blank entries it puts them first. I would like to be able to put them last but can't figure out how to do it. Here is the code that I have at the moment:
set data [$w get 0 end]
set index [lsearch -exact [$w column names] $id]
set result [lsort -dictionary -index $index $data]
$w delete 0 end
# ... and add our sorted data in
eval $w insert end $result
Can anyone help??
Posted by admin (Graham Ellis), 1 September 2003Code:
If you want to sort in a different way to the ways provided, you need to provide a proc to tell Tcl how to know which of two records is logically first - the proc must return -ve, 0, +ve for "they're right as they are", "they are the same" and "they need swapping". When you run lsort with the -command option, you pass it the name of the proc and it will call it internally as many times as it needs to. Neat!
Posted by susyq78 (susyq78), 1 September 2003Thanks for your help - it works except that I had it sorting using the -dictionary command because I had numbers between 1 and 10 to sort and now it doesn't sort them correctly. Silly question, but is there any way you can specify the string compare to work on integers?
Another question that you may be able to help with - I also have a column of dates that I want to format and sort, if the user enters a string in the form DD/MM/YYYY how can I format it as a date and sort on it.
Thanks for all of your help
Posted by admin (Graham Ellis), 2 September 2003Two alternatives - either
a) add an extra section to the comparator - if one value is numeric and the other is not, return either a 1 or -1, if both are numeric use a < or > test, and if neither is numeric stick with the string comparison.
b) Revert to your original code, and afetr sorting use lsearch to find the first non-blank element. You can then use two lranges to divide your list, and then you can rejoin them the other way around.
With the dates, I would use a regular expression match to extract the day, month and year (a very flexible approach which will allow for multiple formats later as you can't guarantee that you user will nicely stick with dd/mm/yyyy), and rejoin the results into a variable of the format yyyymmdd, which is naturally sortable. You also ask about formatting the date - that's going to differ depending on the country and requirement you're formatting for - it could be
2nd September 2003
September 2, 2003
2 sep 2003
and many other formats. With the elements from your regular expression extraction above, you should be able to produce any of these in a few lines of code. By the way - you'll probably want to refer to the date in its "human" form, and other data associated with it, after you've sorted. Trick - hold the data associated with each record in an array, with the array element name being the date formatted for sorting.
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: firstname.lastname@example.org • WEB: http://www.wellho.net • SKYPE: wellho