Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Thu Sep 19, 2019 4:06 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Sorting a date column in a listbox
PostPosted: Mon Nov 23, 2009 11:51 pm 
Offline
User avatar

Joined: Thu Mar 01, 2007 2:02 pm
Posts: 237
Location: Sunny (generally!) Southern California
Listbox cell contents are strings and sort as strings. Thus sorting a column of dates can yield unexpected results.

Whenever I have a listbox that may need to be sorted by date, I create a zero-width column in the listbox and store the SQLDate in that column/row. Then, I have code to handle it in the HeaderPressed event handler.

// this code goes in the listbox's HeaderPressed event handler
// 'column' is passed as a parameter of the event handler,
// 'DateColumn' should be replaced by the column number of the dates
// and 'SQLDateColumn' should be replaced by the column number of the
// SQLDate (zero width) column
// If the user tries to sort by Date, intercept it and sort by SQLDateColumn

Dim i As Integer

// If the user sorts by a non-date column, let the sort happen.
if column <> DateColumn then
me.SortedColumn = column
me.Sort
return true
end if

// We can assume that the user clicked the Date column header
// Get the sort Direction
i = me.ColumnSortDirection (column)

if column = DateColumn then
// Set the sort column to the SQLDate column
// Set the sort direction for the SQLDate column
// Do the Sort
// Set the sort direction for the regular Date column so the
// indicator shows correctly
me.SortedColumn = SQLDateColumn
me.ColumnSortDirection (SQLDateColumn) = i
me.Sort
me.ColumnSortDirection(column) = i
end if
return true



- Dale

_________________
-----
Real Studio 2012r1 on Windows 7 (64 bit)
-----
It has been said that politics is the second oldest profession.
I have learned that it bears a striking resemblance to the first.
- Ronald Reagan


Top
 Profile  
Reply with quote  
 Post subject: Re: Sorting a date column in a listbox
PostPosted: Tue Nov 24, 2009 12:31 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
I find it easier to store the date in the CellTag and implement the CompareRows event.


Top
 Profile  
Reply with quote  
 Post subject: Re: Sorting a date column in a listbox
PostPosted: Tue Nov 24, 2009 10:45 am 
Offline
Site Admin
User avatar

Joined: Fri Sep 30, 2005 9:35 am
Posts: 987
Location: South Portland, Maine
Yes, using the CompareRows event for this is nice and clean:

If column = 0 Then // The column containing your dates
Dim d1 As Date
Dim d2 As Date

d1 = Date(Me.CellTag(row1, column))
d2 = Date(Me.CellTag(row2, column))

If d1.TotalSeconds > d2.TotalSeconds Then
result = 1
ElseIf d1.TotalSeconds < d2.TotalSeconds Then
result = -1
Else
result = 0
End If

Return True
End If

Return False // Use default string-based sorting

_________________
Paul Lefebvre
Developer Evangelist
Xojo, Inc.


Top
 Profile  
Reply with quote  
 Post subject: Re: Sorting a date column in a listbox
PostPosted: Tue Nov 24, 2009 11:56 pm 
Offline
User avatar

Joined: Thu Mar 01, 2007 2:02 pm
Posts: 237
Location: Sunny (generally!) Southern California
I would also except that I find myself frequently using the CellTag for another purpose when I have date columns. Using the CellTags and CompareRows certainly makes numeric and currency sorts easier.

- Dale

_________________
-----
Real Studio 2012r1 on Windows 7 (64 bit)
-----
It has been said that politics is the second oldest profession.
I have learned that it bears a striking resemblance to the first.
- Ronald Reagan


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group