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. Problem w/ CListCtrl::SortItems

Problem w/ CListCtrl::SortItems

Scheduled Pinned Locked Moved C / C++ / MFC
helpalgorithmsquestion
2 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.
  • D Offline
    D Offline
    De Nardis Andrea
    wrote on last edited by
    #1

    Did anyone experience any problems using CListCtrl::SortItems? I take this code from MSDN for the sorting callback function but it does not work. // Sort the item in reverse alphabetical order. static int CALLBACK MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { // lParamSort contains a pointer to the list view control. CListCtrl* pListCtrl = (CListCtrl*) lParamSort; CString strItem1 = pListCtrl->GetItemText(lParam1, 0); CString strItem2 = pListCtrl->GetItemText(lParam2, 0); return strcmp(strItem2, strItem1); } The result of this code when I apply to my list is that MyCompareProc is called exactly as many times as the item number but this is clearly not sufficient for a complet alphabetical order!!! Thus the order is wrong. Please help me!!!:(( Regards, Andrea

    D 1 Reply Last reply
    0
    • D De Nardis Andrea

      Did anyone experience any problems using CListCtrl::SortItems? I take this code from MSDN for the sorting callback function but it does not work. // Sort the item in reverse alphabetical order. static int CALLBACK MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { // lParamSort contains a pointer to the list view control. CListCtrl* pListCtrl = (CListCtrl*) lParamSort; CString strItem1 = pListCtrl->GetItemText(lParam1, 0); CString strItem2 = pListCtrl->GetItemText(lParam2, 0); return strcmp(strItem2, strItem1); } The result of this code when I apply to my list is that MyCompareProc is called exactly as many times as the item number but this is clearly not sufficient for a complet alphabetical order!!! Thus the order is wrong. Please help me!!!:(( Regards, Andrea

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

      De Nardis Andrea wrote: ...MyCompareProc is called exactly as many times as the item number but this is clearly not sufficient for a complet alphabetical order That depends on the sorting algorithm used. While some algorithms look at each item multiple times (e.g., bubble), others look at each item fewer times, or even just once. I suspect that the algorithm employed by SortItems() is based on Hoare's partition-exchange (i.e., quicksort) algorithm. As you add items to the control, a call to SetItemData() should be made. For example, the item's data could be a pointer to a CMyObject class. Then the comparison routine will look like:

      static int CALLBACK
      MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
      {
      CMyObject *p1 = (CMyObject *) lParam1;
      CMyObject *p2 = (CMyObject *) lParam2;
      CString str1 = p1->SomeString;
      CString str2 = p2->SomeString;

      return strcmp(str1, str2);
      

      }


      Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

      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