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 using CListCtrl::SortItems

Sorting using CListCtrl::SortItems

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++comalgorithmsdebugging
5 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.
  • J Offline
    J Offline
    JJeffrey
    wrote on last edited by
    #1

    I'm having a problem using this function of the MFC List Control class I've read the article : Example of CListCtrl::SortItems(...) in MSDN By Ivor S. Sargoytchev Example of CListCtrl::SortItems(...) in MSDN[^] And am trying out his example Callback funtion this way:

    static int CALLBACK CompareProc(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 _tcscmp(strItem1, strItem2); // This should do ascending Sort, I believe?
    }

    Relevant Code:

    LVCOLUMN lvColumn;
    int nCol =0;

    lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
    lvColumn.fmt = LVCFMT_LEFT;
    lvColumn.cx = 120;
    lvColumn.pszText = ColumnHeading[0]; //<-- Global Enum list of titles
    nCol = m_MainList.InsertColumn(0, &lvColumn);

    int NextIndex = nCol;
    .
    .
    .
    TCHAR Output[50]={0};
    while ()
    {

    LVITEM lvItem;
    lvItem.mask = LVIF_TEXT;
    lvItem.iItem = NextIndex;
    lvItem.iSubItem = 0;
    lvItem.pszText = Output;
    NextIndex = m_MainList.InsertItem(&lvItem);
    }

    m_MainList.SortItems(CompareProc, (LPARAM)&m_MainList);

    m_MainList is the Control variable for a List Control box I placed on a dialog I put a breakpoint inside the Sort Function CompareProc and found that lParam1 and lParam2 are always 0 constantly and never increasing. I have been told it could be a pointer issue, but I don't get it. Can anyone help? Thanks

    N D 2 Replies Last reply
    0
    • J JJeffrey

      I'm having a problem using this function of the MFC List Control class I've read the article : Example of CListCtrl::SortItems(...) in MSDN By Ivor S. Sargoytchev Example of CListCtrl::SortItems(...) in MSDN[^] And am trying out his example Callback funtion this way:

      static int CALLBACK CompareProc(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 _tcscmp(strItem1, strItem2); // This should do ascending Sort, I believe?
      }

      Relevant Code:

      LVCOLUMN lvColumn;
      int nCol =0;

      lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
      lvColumn.fmt = LVCFMT_LEFT;
      lvColumn.cx = 120;
      lvColumn.pszText = ColumnHeading[0]; //<-- Global Enum list of titles
      nCol = m_MainList.InsertColumn(0, &lvColumn);

      int NextIndex = nCol;
      .
      .
      .
      TCHAR Output[50]={0};
      while ()
      {

      LVITEM lvItem;
      lvItem.mask = LVIF_TEXT;
      lvItem.iItem = NextIndex;
      lvItem.iSubItem = 0;
      lvItem.pszText = Output;
      NextIndex = m_MainList.InsertItem(&lvItem);
      }

      m_MainList.SortItems(CompareProc, (LPARAM)&m_MainList);

      m_MainList is the Control variable for a List Control box I placed on a dialog I put a breakpoint inside the Sort Function CompareProc and found that lParam1 and lParam2 are always 0 constantly and never increasing. I have been told it could be a pointer issue, but I don't get it. Can anyone help? Thanks

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      JJeffrey wrote:

      I put a breakpoint inside the Sort Function CompareProc and found that lParam1 and lParam2 are always 0 constantly and never increasing.

      When you use the SortItems() function, the lParam1 and lParam2 passed to the CompareProc are not item indexes, but Item data corrsponding to each item in the list control. I must says its bit difficult to sort if you use the SortItems() function. Sortitems() wrapper the message LVM_SORTITEMS. But there is another message called LVM_SORTITEMSEX, which will work as you expected. So instead of m_MainList.SortItems(CompareProc, (LPARAM)&m_MainList);, call ListView_SortItemsEx( m_MainList.m_hWnd, CompareProc, (LPARAM)&m_MainList );

      nave [OpenedFileFinder] [My Blog]

      J 1 Reply Last reply
      0
      • N Naveen

        JJeffrey wrote:

        I put a breakpoint inside the Sort Function CompareProc and found that lParam1 and lParam2 are always 0 constantly and never increasing.

        When you use the SortItems() function, the lParam1 and lParam2 passed to the CompareProc are not item indexes, but Item data corrsponding to each item in the list control. I must says its bit difficult to sort if you use the SortItems() function. Sortitems() wrapper the message LVM_SORTITEMS. But there is another message called LVM_SORTITEMSEX, which will work as you expected. So instead of m_MainList.SortItems(CompareProc, (LPARAM)&m_MainList);, call ListView_SortItemsEx( m_MainList.m_hWnd, CompareProc, (LPARAM)&m_MainList );

        nave [OpenedFileFinder] [My Blog]

        J Offline
        J Offline
        JJeffrey
        wrote on last edited by
        #3

        Thank you. That solved it. So it's mostly Microsoft's fault then? For coming up with that stupid function? Really. Thanks. Was working on trying to make it work for a day now.

        N 1 Reply Last reply
        0
        • J JJeffrey

          Thank you. That solved it. So it's mostly Microsoft's fault then? For coming up with that stupid function? Really. Thanks. Was working on trying to make it work for a day now.

          N Offline
          N Offline
          Naveen
          wrote on last edited by
          #4

          You are welcome.

          JJeffrey wrote:

          So it's mostly Microsoft's fault then? For coming up with that stupid function?

          During te sorting process, the list-view contents are unstable. So in the intial versions of common control, I guess they might have felt some difficulty is passing the item indexes to the compare function. Later from the comctl32.dll version 5.80, they added the support for the LVM_SORTITEMSEX message. They might have found some work around later.. :)

          nave [OpenedFileFinder] [My Blog]

          1 Reply Last reply
          0
          • J JJeffrey

            I'm having a problem using this function of the MFC List Control class I've read the article : Example of CListCtrl::SortItems(...) in MSDN By Ivor S. Sargoytchev Example of CListCtrl::SortItems(...) in MSDN[^] And am trying out his example Callback funtion this way:

            static int CALLBACK CompareProc(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 _tcscmp(strItem1, strItem2); // This should do ascending Sort, I believe?
            }

            Relevant Code:

            LVCOLUMN lvColumn;
            int nCol =0;

            lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
            lvColumn.fmt = LVCFMT_LEFT;
            lvColumn.cx = 120;
            lvColumn.pszText = ColumnHeading[0]; //<-- Global Enum list of titles
            nCol = m_MainList.InsertColumn(0, &lvColumn);

            int NextIndex = nCol;
            .
            .
            .
            TCHAR Output[50]={0};
            while ()
            {

            LVITEM lvItem;
            lvItem.mask = LVIF_TEXT;
            lvItem.iItem = NextIndex;
            lvItem.iSubItem = 0;
            lvItem.pszText = Output;
            NextIndex = m_MainList.InsertItem(&lvItem);
            }

            m_MainList.SortItems(CompareProc, (LPARAM)&m_MainList);

            m_MainList is the Control variable for a List Control box I placed on a dialog I put a breakpoint inside the Sort Function CompareProc and found that lParam1 and lParam2 are always 0 constantly and never increasing. I have been told it could be a pointer issue, but I don't get it. Can anyone help? Thanks

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

            For future reference, when adding items to the control, you'll also need to call SetItemData(). This is because SortItems() internally calls GetItemData() (or an equivalent) before calling your comparison routine.

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            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