Sorting date with CListCtrl
-
Hi all, My List control displays Timestamp along with date (15/6/2004 12:02:02 AM).. My code to sort this data is.... int CALLBACK CBoseDayalaNewListCtrl::CompareDates(LPARAM lParam1, LPARAM lParam2, LPARAM lSortParam) { CString str1 = *((CString*)(((ItemData*)lParam1)->dwItemData)); CString str2 = *((CString*)(((ItemData*)lParam2)->dwItemData)); COleDateTime odtTime1; COleDateTime odtTime2; BOOL fSortAscending = BOOL(lSortParam); double dblResult = 0.0; odtTime1.ParseDateTime( str1, LOCALE_NOUSEROVERRIDE ); odtTime2.ParseDateTime( str2, LOCALE_NOUSEROVERRIDE ); if(fSortAscending) dblResult = odtTime1 - odtTime2; else dblResult = odtTime2 - odtTime1; return ( ( 0.0 < dblResult ) ? 1 : ( ( 0.0 == dblResult ) ? 0 : -1 ) ); } ....and this doesn't work fine it seems...can anybody gimme more efficient code so that I will be so grateful... thanks in advance. MrBoseDayala
-
Hi all, My List control displays Timestamp along with date (15/6/2004 12:02:02 AM).. My code to sort this data is.... int CALLBACK CBoseDayalaNewListCtrl::CompareDates(LPARAM lParam1, LPARAM lParam2, LPARAM lSortParam) { CString str1 = *((CString*)(((ItemData*)lParam1)->dwItemData)); CString str2 = *((CString*)(((ItemData*)lParam2)->dwItemData)); COleDateTime odtTime1; COleDateTime odtTime2; BOOL fSortAscending = BOOL(lSortParam); double dblResult = 0.0; odtTime1.ParseDateTime( str1, LOCALE_NOUSEROVERRIDE ); odtTime2.ParseDateTime( str2, LOCALE_NOUSEROVERRIDE ); if(fSortAscending) dblResult = odtTime1 - odtTime2; else dblResult = odtTime2 - odtTime1; return ( ( 0.0 < dblResult ) ? 1 : ( ( 0.0 == dblResult ) ? 0 : -1 ) ); } ....and this doesn't work fine it seems...can anybody gimme more efficient code so that I will be so grateful... thanks in advance. MrBoseDayala
What do mean by "it doesn't work fine"? Does it not work at all or is it slow and inefficient? If it's inefficent, maybe store pointers to the COleDateTime in the data address of the items, rather than CString pointers that you need to convert every time you do a sort. Another option would be that at the time you are inserting items into the listctrl, the dwData(param) could be a
time_t
object. This is just a typedef'd long. That way you don't need to allocate memory for CStrings or COleDateTime objects, and stil do the comparisons.
-
What do mean by "it doesn't work fine"? Does it not work at all or is it slow and inefficient? If it's inefficent, maybe store pointers to the COleDateTime in the data address of the items, rather than CString pointers that you need to convert every time you do a sort. Another option would be that at the time you are inserting items into the listctrl, the dwData(param) could be a
time_t
object. This is just a typedef'd long. That way you don't need to allocate memory for CStrings or COleDateTime objects, and stil do the comparisons.
it is not working efficient...I have send the picture to u.please have a look.. BoseDayala
-
it is not working efficient...I have send the picture to u.please have a look.. BoseDayala
Hi Bose, the only thing I can see that could be incorrect, is that the ItemData that you are pointing to, has the incorrect values in it. I would debug through it, and step through and check the value (dates/time/strings) that you are using. I did a quick test, and used pointers to CStrings as the lParam in each item, and used almost the same function as you in the comparison. It seemed to work fine. just remember that I didn't have a ItemData structure, I used pointers to CStrings that held the date/times. Jubjub
-
it is not working efficient...I have send the picture to u.please have a look.. BoseDayala
Mr Bose Dayala wrote: it is not working efficient So it is working, just not very fast?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
-
Mr Bose Dayala wrote: it is not working efficient So it is working, just not very fast?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
efficient means...some dates are not being sorted properly... ThomasKennedyBose
-
efficient means...some dates are not being sorted properly... ThomasKennedyBose
A sorting algorithm either sorts or it doesn't. There is no "some." How are you adding items to the list control? What parameters are you calling
SetItemData()
with? The0.0 == dblResult
comparison is obviously wrong. You should never comparedouble
orfloat
variables in this fashion. Hopefully you meant for the comparision to look likereturn ((0.0 < dblResult) ? 1 : ((dblResult < 0.0) ? -1 : 0))
, but this will still lead to undesirable results, becausedblResult
will always be greater than or less than 0. A more elegant solution would be: int CALLBACK CBoseDayalaNewListCtrl::CompareDates(LPARAM lParam1, LPARAM lParam2, LPARAM lSortParam) { CString *str1 = (CString*)(((ItemData*)lParam1)->dwItemData); CString *str2 = (CString*)(((ItemData*)lParam2)->dwItemData); CString *temp; COleDateTime odtTime1; COleDateTime odtTime2; BOOL fSortAscending = BOOL(lSortParam); int nResult = 0; if (fSortAscending) { temp = str1; str1 = str2; str2 = temp; } TRACE("Comparing %s to %s\n", str1.Format("%c"), str2.Format("%c")); odtTime1.ParseDateTime( str1, LOCALE_NOUSEROVERRIDE ); odtTime2.ParseDateTime( str2, LOCALE_NOUSEROVERRIDE ); if (odtTime1 < odtTime2) nResult = -1; else nResult = 1; return nResult; }
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen