Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Sorting date with CListCtrl

Sorting date with CListCtrl

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmsquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mr Bose Dayala
    wrote on last edited by
    #1

    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

    V 1 Reply Last reply
    0
    • M Mr Bose Dayala

      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

      V Offline
      V Offline
      Vadim Tabakman
      wrote on last edited by
      #2

      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.

      M 1 Reply Last reply
      0
      • V Vadim Tabakman

        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.

        M Offline
        M Offline
        Mr Bose Dayala
        wrote on last edited by
        #3

        it is not working efficient...I have send the picture to u.please have a look.. BoseDayala

        V D 2 Replies Last reply
        0
        • M Mr Bose Dayala

          it is not working efficient...I have send the picture to u.please have a look.. BoseDayala

          V Offline
          V Offline
          Vadim Tabakman
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • M Mr Bose Dayala

            it is not working efficient...I have send the picture to u.please have a look.. BoseDayala

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            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

            M 1 Reply Last reply
            0
            • D David Crow

              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

              M Offline
              M Offline
              Mr Bose Dayala
              wrote on last edited by
              #6

              efficient means...some dates are not being sorted properly... ThomasKennedyBose

              D 1 Reply Last reply
              0
              • M Mr Bose Dayala

                efficient means...some dates are not being sorted properly... ThomasKennedyBose

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                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? The 0.0 == dblResult comparison is obviously wrong. You should never compare double or float variables in this fashion. Hopefully you meant for the comparision to look like return ((0.0 < dblResult) ? 1 : ((dblResult < 0.0) ? -1 : 0)), but this will still lead to undesirable results, because dblResult 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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups