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 Error in CListCtrl

Sorting Error in CListCtrl

Scheduled Pinned Locked Moved C / C++ / MFC
helpdatabasealgorithms
3 Posts 2 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.
  • P Offline
    P Offline
    pc_dev
    wrote on last edited by
    #1

    ClistCtrl has SortItems function that allows us to use an application defined callback function, the problem is that inside that function (MyCompareProc), lParam1 and lParam2 are always same so there is no sorting sice both represent same data. Here is code from MSDN. static int CALLBACK MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { // lParamSort contains a pointer to the list view control. // The lParam of an item is just its index. CListCtrl* pListCtrl = (CListCtrl*) lParamSort; CString strItem1 = pListCtrl->GetItemText(lParam1,0); CString strItem2 = pListCtrl->GetItemText(lParam2,0); return strcmp(strItem2, strItem1); } void snip_CListCtrl_SortItems() { // The pointer to my list view control. CListCtrl* pmyListCtrl; // Sort the list view items using my callback procedure. pmyListCtrl->SortItems(MyCompareProc,(LPARAM)myListCtrl);

    P 1 Reply Last reply
    0
    • P pc_dev

      ClistCtrl has SortItems function that allows us to use an application defined callback function, the problem is that inside that function (MyCompareProc), lParam1 and lParam2 are always same so there is no sorting sice both represent same data. Here is code from MSDN. static int CALLBACK MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { // lParamSort contains a pointer to the list view control. // The lParam of an item is just its index. CListCtrl* pListCtrl = (CListCtrl*) lParamSort; CString strItem1 = pListCtrl->GetItemText(lParam1,0); CString strItem2 = pListCtrl->GetItemText(lParam2,0); return strcmp(strItem2, strItem1); } void snip_CListCtrl_SortItems() { // The pointer to my list view control. CListCtrl* pmyListCtrl; // Sort the list view items using my callback procedure. pmyListCtrl->SortItems(MyCompareProc,(LPARAM)myListCtrl);

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      pc_dev wrote:

      ), lParam1 and lParam2 are always same so there is no sorting sice both represent same data.

      The MSDN example given is wrong.

      pc_dev wrote:

      static int CALLBACK MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { // lParamSort contains a pointer to the list view control. // The lParam of an item is just its index. CListCtrl* pListCtrl = (CListCtrl*) lParamSort;

      Here lParam1 and lParam2 are the item data for these two items. These needs to be set by using SetItemData. Refer this[^] article by Ivor S. Sargoytchev.


      Prasad MS MVP -  VC++

      P 1 Reply Last reply
      0
      • P prasad_som

        pc_dev wrote:

        ), lParam1 and lParam2 are always same so there is no sorting sice both represent same data.

        The MSDN example given is wrong.

        pc_dev wrote:

        static int CALLBACK MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { // lParamSort contains a pointer to the list view control. // The lParam of an item is just its index. CListCtrl* pListCtrl = (CListCtrl*) lParamSort;

        Here lParam1 and lParam2 are the item data for these two items. These needs to be set by using SetItemData. Refer this[^] article by Ivor S. Sargoytchev.


        Prasad MS MVP -  VC++

        P Offline
        P Offline
        pc_dev
        wrote on last edited by
        #3

        Thanks :rose:

        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