Solved

Sorting with CListView and LVN_GETDISPINFO

Posted on 2004-09-22
5
676 Views
Last Modified: 2013-11-20
Hi,

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);
      
       NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)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;
      else
            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);
      else
            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.
0
Comment
Question by:Netikus
  • 2
  • 2
5 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 300 total points
ID: 12130545
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.
0
 
LVL 1

Author Comment

by:Netikus
ID: 12134295
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.


Thanks!
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12136171
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.)
0
 
LVL 6

Assisted Solution

by:Amritpal Singh
Amritpal Singh earned 200 total points
ID: 12141522
0
 
LVL 1

Author Comment

by:Netikus
ID: 12154990
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.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now