Sorting with CListView and LVN_GETDISPINFO


I have experience with MFC and list views, but this is the first time I have to tackle sorting output and I'm having difficulties understanding how to do it.

I have 2 views, one of which (the easier one) I got sorting to work OK with - and now I would like to use similar code to get the other one to work. So the first list is basically a list of computer items, and I added the following code (the rest of the class is omitted for simplicity):

static BOOL bSortAscending;
static int iColumn;

void CRemoteUpdateView::OnLvnColumnclick(NMHDR *pNMHDR, LRESULT *pResult)
      LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
       iColumn = pNMListView->iSubItem;                        // Column starting with 0

      for (int i = 0; i < listCtrl.GetItemCount(); i++)
            listCtrl.SetItemData(i, i);

      listCtrl.SortItems(CompareItems, (LPARAM) &listCtrl);

      if (bSortAscending)
            bSortAscending = FALSE;
            bSortAscending = TRUE;

      *pResult = 0;

static int CALLBACK CompareItems(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
      int iReturn = 0;

// lParamSort contains a pointer to the list view control.
      CListCtrl* pListCtrl = (CListCtrl*) lParamSort;

      CString strItem1 = pListCtrl->GetItemText(lParam1, iColumn);
      CString strItem2 = pListCtrl->GetItemText(lParam2, iColumn);

      if (bSortAscending)
            iReturn = strcmp(strItem2, strItem1);
            iReturn = (strcmp(strItem2, strItem1) * -1);

      return iReturn;

Now, I have to admit that I don't 100% understand it, especially the

      for (int i = 0; i < listCtrl.GetItemCount(); i++)
            listCtrl.SetItemData(i, i);

part. Why do I need to assign numeric values to those items?

Anyhow, the 2nd view is different as I am using LVN_GETDISPINFO rather than inserting items with InsertItem() for speed reasons. This view can sometimes have 100.000 items in it.

So I have a memory structure that holds the information pulled by the OnGetdispinfo() function.

Now I can't use the code I used for the other view, since I can't call SetItemData() anymore and I'm a bit stuck.

If somebody can point out how to do this and also explain how the sorting feature works? It is my understanding the the lParam member of the item is what tells the SortItems() function the sequence, but what if I use LVN_GETDISPINFO?

Thank you.
Who is Participating?
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
The first question - the SetItemData.  That is just putting a unique value to each item in the list.

Your second case - called a virtual list by the way - you will have to sort the contents of your internal structure yourself in response to the header click event.
IF it comes from a database the quickest sort may be to rerun the query with the new sort and refill your internal structure again.
NetikusAuthor Commented:
OK. I understand that I am just putting a unique item in the list. Is it necessary to specify sequential numbers - I assume it is - right? Why doesn't it automatically go through the list one-by-one from the top to bottom - this would seem logical to me rather than calling SetItemData. What happens if one is already using the lParam parameter of the item structures (which I am doing in other cases) for other purposes - wouldn't those be overwritten?

The virtual list makes sense of course. I will work on that asap.

In the virtual list the data is actually stored in data structures, so sorting that will be a bit more difficult since it's not coming from a database. I'll do some research on sorting algorithms since I haven't dealt with sorting in a long time.

AndyAinscowFreelance programmer / ConsultantCommented:
First point re numbers/SetItemData - I haven't studied that piece of code so I don't know why they are assigning a sequential number.  If you are already using that for storage of other info then ignore the assignment lines.  

The compare items should just return a value to say if the items should be swapped.  (It is a very simple but inefficient sorting mechanism used there by default.)
Amritpal SinghConnect With a Mentor Commented:
NetikusAuthor Commented:
Thanks a lot guys, both the answers helped me finally understand and solve this.

For the large virtual list I had to refer to an algorithm theory book and use Quicksort, which works very nicely and fast - even for 20.000 items and more.

I also understand now that it is through the values of lParam that the compare function knows which items to compare. As such, the lParam values are integral for sorting. Without them set properly, the compare algorithm does not know which items to compare.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.